#include <bits/stdc++.h>
using namespace std;
const int inf = (int)2e9, N = 200 * 1000 + 101;
int t1[4 * N], t2[4 * N];
int to_st(int n) {
  int pow = 6;
  for (int i = 2; i <= n; i++) {
    pow *= 6;
  }
  return pow;
}
map<string, char> m;
bool check(string s, int n) {
  for (int i = 0; i < n - 1; i++) {
    string now = "";
    now += s[0];
    now += s[1];
    map<string, char>::iterator it = m.find(now);
    if (it == m.end()) return false;
    s.erase(s.begin());
    s.erase(s.begin());
    s = (*it).second + s;
  }
  if (s == "a") return true;
  return false;
}
int main() {
  int n, q;
  cin >> n >> q;
  int maxN = to_st(n);
  for (int i = 0; i < q; i++) {
    string s;
    char c;
    cin >> s >> c;
    m[s] = c;
  }
  int ans = 0;
  for (int i = 0; i < maxN; i++) {
    vector<int> mask(n, 0);
    int cur = i, j = 0;
    while (cur > 0) {
      mask[j] = cur % 6;
      cur /= 6;
      j++;
    }
    string s = "";
    for (int k = 0; k < n; k++) {
      s += 'a' + mask[k];
    }
    if (check(s, n) == true) {
      ans++;
    }
  }
  cout << ans;
  return 0;
}
