#include <bits/stdc++.h>
using namespace std;
const int inf = (1 << 30) - 1;
const long long linf = (1ll << 62) - 1;
const int N = 1e5 + 100;
int n, m;
string s1[N], s2[N];
char s[N];
int check(string cur) {
  if (cur == "a") {
    return 1;
  }
  if (((int)(cur).size()) <= 1) {
    return 0;
  }
  for (int i = 0; i < m; i++) {
    if (cur[0] == s1[i][0] && cur[1] == s1[i][1]) {
      string t = cur;
      cur.erase(cur.begin());
      cur.erase(cur.begin());
      cur.insert(cur.begin(), (s2[i]).begin(), (s2[i]).end());
      if (check(cur)) {
        return true;
      }
      cur = t;
    }
  }
  return false;
}
int ans = 0;
void make(int x) {
  if (x >= n) {
    return void(ans += check(string(s, s + n)));
  }
  for (char c = 'a'; c <= 'f'; c++) {
    s[x] = c;
    make(x + 1);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s1[i] >> s2[i];
  }
  make(0);
  cout << ans << endl;
  return 0;
}
