#include <bits/stdc++.h>
using namespace std;
map<string, char> mm;
int ans, n;
void dfs(char now, int len) {
  if (len == n) {
    ans++;
    return;
  }
  for (map<string, char>::iterator it = mm.begin(); it != mm.end(); it++) {
    char get = it->second;
    if (get == now) {
      string a = it->first;
      dfs(a[0], len + 1);
    }
  }
}
int main() {
  int q;
  scanf("%d%d", &n, &q);
  while (q--) {
    string a;
    cin >> a;
    char ch;
    cin >> ch;
    mm[a] = ch;
  }
  ans = 0;
  dfs('a', 1);
  cout << ans << endl;
  return 0;
}
