文件目录

#include <bits/stdc++.h>
using namespace std;
int n, k, x[20], y[20];
int num[10];
int bfs(int xx)
{
    int ans = 1;
    bool vis[10] = {};
    vis[xx] = true;
    queue<int> q;
    q.push(xx);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < k; i++)
        {
            if (x[i] == u && vis[y[i]] == false)
            {
                q.push(y[i]);
                ans++;
                vis[y[i]] = true;
            }
        }
    }
    return ans;
}
int main()
{
    cin >> n >> k;
    for (int i = 0; i < k; i++)
        cin >> x[i] >> y[i];
    for (int i = 0; i < 10; i++)
        num[i] = bfs(i);

    // for(int i=1;i<10;i++) cout<<num[i]<<" ";cout<<endl;

    int sum = 1;
    while (n)
    {
        sum *= num[n % 10];
        n /= 10;
    }
    cout << sum;
    return 0;
}