#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int n, m, adj[N][N], ans;
vector<string> v;
string curr;
map<string, string> mp;
void fact(int i) {
  if (i == n) {
    v.push_back(curr);
    return;
  }
  for (int j = 0; j < 6; j++) {
    curr += (char)('a' + j);
    fact(i + 1);
    curr.pop_back();
  }
}
bool ok(string s) {
  int sz = s.length();
  if (sz == 1) return s == "a";
  string curr = "";
  curr += s[0];
  curr += s[1];
  if (!mp.count(curr)) return 0;
  curr = mp[curr];
  for (int i = 2; i < sz; i++) curr += s[i];
  return ok(curr);
}
int main() {
  cin >> n >> m;
  fact(0);
  for (int i = 0; i < m; i++) {
    string a, b;
    cin >> a >> b;
    mp[a] = b;
  }
  for (auto x : v) ans += ok(x);
  printf("%d", ans);
}
