#include <bits/stdc++.h>
using namespace std;
map<string, char> mp;
int cnt;
bool check(string s) {
  char ch = s[0];
  for (int j = 1; j < s.length(); j++) {
    string s1;
    s1.push_back(ch), s1.push_back(s[j]);
    if (mp.find(s1) != mp.end())
      ch = mp[s1];
    else
      return false;
  }
  if (ch == 'a')
    return true;
  else
    return false;
}
void find(int ind, string s) {
  if (ind == -1) {
    if (check(s)) cnt++;
    return;
  } else {
    for (char ch = 'a'; ch <= 'f'; ch++) {
      s[ind] = ch;
      find(ind - 1, s);
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= q; i++) {
      string x;
      char y;
      cin >> x;
      cin >> y;
      mp[x] = y;
    }
    string s;
    for (int i = 0; i < n; i++) s.push_back('a');
    find(s.length() - 1, s);
    cout << cnt;
  }
  return 0;
}
