#include <bits/stdc++.h>
using namespace std;
map<string, string> p;
int ans = 0;
string tmp = "abcdef";
void doit(int n, string s) {
  if (n == 0) {
    int f = 0;
    while (true) {
      if (s.length() == 2) {
        if (p.find(s) == p.end()) {
          f = 1;
          break;
        }
        if (p[s] != "a") {
          f = 1;
        }
        break;
      }
      string s1 = s.substr(0, 2);
      string s2 = "";
      if (s.length() > 2) {
        s2 += s.substr(2, s.length() - 2);
      }
      if (p.find(s1) == p.end()) {
        f = 1;
        break;
      } else {
        s = p[s1] + s2;
      }
    }
    if (f == 0) ans++;
    return;
  }
  for (int i = 0; i < 6; i++) {
    string tmp1 = s;
    tmp1.push_back(tmp[i]);
    doit(n - 1, tmp1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    p.insert(make_pair(s1, s2));
  }
  for (int i = 0; i < 6; i++) {
    string s;
    s.push_back(tmp[i]);
    doit(n - 1, s);
  }
  cout << ans;
  return 0;
}
