#include <bits/stdc++.h>
const int maxn = 4007;
std::vector<char> lis;
struct Node {
  char a[11], b;
} node[maxn];
int n, q;
int dfs(char s, int len) {
  if (len == n) return 1;
  int ret = 0;
  for (int i = 1; i <= q; i++) {
    if (node[i].b == s) ret += dfs(node[i].a[0], len + 1);
  }
  return ret;
}
int main() {
  std::cin >> n >> q;
  for (int i = 1; i <= q; i++) {
    std::cin >> node[i].a >> node[i].b;
  }
  long long ans[11] = {0ll};
  std::cout << dfs('a', 1) << "\n";
}
