#1,#2:AC;
#3:MLE;
#4,#5:TLE;
Code:
#include <unordered_map>
#include <iostream>
#include <vector>
#include <string>
int k,x,y;
std::string n;
__int128 ans = 1;
std::unordered_map <std::string,bool> appered;
std::unordered_map <int,std::vector <int> > ds;
void write(auto x){
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
void dfs(std::string x){
for (char &c : x){
char tmp = c;
if (ds.find(c - '0') != ds.end())
for (int i = 0;i < ds[c - '0'].size();i++){
c = ds[c - '0'][i] + '0';
if (!appered[x])
appered[x] = true,ans++,dfs(x);
c = tmp;
}
}
}
int main(){
std::cin >> n >> k;
appered[n] = true;
while (k--) std::cin >> x >> y,ds[x].push_back(y);
dfs(n);
write(ans);
return 0;
}