#include <bits/stdc++.h>
using namespace std;
int n, q, mem[300][10];
map<char, vector<char>> mp;
int slv(char c, int s) {
  if (s == n) return 1;
  if (mem[c][s] != -1) return mem[c][s];
  int ans = 0;
  for (char nc : mp[c]) ans += slv(nc, s + 1);
  return mem[c][s] = ans;
}
int main() {
  memset(mem, -1, sizeof mem);
  char s1[5], s2;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < q; ++i) {
    scanf("%s %c", s1, &s2);
    mp[s2].push_back(s1[0]);
  }
  printf("%d", slv('a', 1));
  return 0;
}
