#include <bits/stdc++.h>
using namespace std;
vector<string> sequences;
map<string, char> subs;
string symbols = "abcdef";
int n, q;
void generate(int pos, string str) {
  if (pos == n) {
    sequences.push_back(str);
    return;
  }
  for (int i = 0; i < symbols.size(); i++) {
    generate(pos + 1, str + symbols[i]);
  }
}
bool check(string s) {
  while (s.size() > 1) {
    string temp;
    temp += s[0];
    temp += s[1];
    if (subs[temp]) {
      s.erase(0, 1);
      s[0] = subs[temp];
    } else
      return 0;
  }
  if (s[0] == 'a')
    return 1;
  else
    return 0;
}
int main() {
  cin >> n >> q;
  string a;
  char b;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    subs[a] = b;
  }
  generate(0, "");
  int sum = 0;
  for (int i = 0; i < sequences.size(); i++) {
    if (check(sequences[i])) sum++;
  }
  cout << sum;
}
