#include <bits/stdc++.h>
using namespace std;
int v[60];
char aa[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
map<string, char> m;
long long solve(string s) {
  if (s.size() == 1 && s[0] == 'a') return 1;
  if (s.size() == 1) return 0;
  string pre = s.substr(0, 2);
  string pos = "";
  if (s.size() > 2) pos = s.substr(2, s.size());
  if (m[pre]) {
    return solve(m[pre] + pos);
  }
  return 0;
}
long long generate2(int n, string s) {
  if (n == 0) {
    return solve(s);
  }
  long long a = 0;
  for (int i = 0; i < 6; i++) {
    a += generate2(n - 1, s + aa[i]);
  }
  return a;
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < q; ++i) {
    string a;
    char b;
    cin >> a >> b;
    m[a] = b;
  }
  cout << generate2(n, "") << endl;
  return 0;
}
