#include <bits/stdc++.h>
using namespace std;
string s = "abcdef";
int rec(string fs, int c, map<string, string>& m, int n) {
  if (fs.length() == 2) {
    if (m.count(fs))
      fs = m[fs];
    else
      return 0;
  }
  if (c == n) {
    if (fs == "a")
      return 1;
    else
      return 0;
  }
  int count = 0;
  for (int i = 0; i < 6; i++) {
    count += rec(fs + s[i], c + 1, m, n);
  }
  return count;
}
int main() {
  int n, q;
  cin >> n >> q;
  string a, b;
  map<string, string> m;
  while (q--) {
    cin >> a >> b;
    m[a] = b;
  }
  string fs = "";
  cout << rec(fs, 0, m, n);
  return 0;
}
