#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5, MOD = 1e9 + 7;
char a[40][3];
char b[40][2];
vector<string> V[40];
int n, q;
bool valid(string m) {
  char s[10];
  for (int i = 0; i < m.length(); i++) s[i] = m[i];
  int len = n;
  while (len > 1) {
    bool ok = false;
    for (int i = 0; i < q; i++) {
      if (s[0] == a[i][0] and s[1] == a[i][1]) {
        s[1] = b[i][0];
        for (int j = 1; j < len; j++) s[j - 1] = s[j];
        s[len - 1] = '\0';
        len--;
        ok = true;
        break;
      }
    }
    if (!ok) return false;
  }
  if (s[0] == 'a' and s[1] == '\0') {
    return 1;
  }
  return false;
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i];
    cin >> b[i];
  }
  V[1].push_back("a");
  V[1].push_back("b");
  V[1].push_back("c");
  V[1].push_back("d");
  V[1].push_back("e");
  V[1].push_back("f");
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < V[i - 1].size(); j++) {
      string s = "a" + V[i - 1][j];
      V[i].push_back(s);
      s = "b" + V[i - 1][j];
      V[i].push_back(s);
      s = "c" + V[i - 1][j];
      V[i].push_back(s);
      s = "d" + V[i - 1][j];
      V[i].push_back(s);
      s = "e" + V[i - 1][j];
      V[i].push_back(s);
      s = "f" + V[i - 1][j];
      V[i].push_back(s);
    }
  }
  int ans = 0;
  for (int i = 0; i < V[n].size(); i++) {
    ans += valid(V[n][i]);
  }
  cout << ans << endl;
}
