#include <bits/stdc++.h>
using namespace std;
int n, q;
int total = 0;
void recurse(string a[], string b[], string s) {
  if (s.length() == 1 && s[0] == 'a') {
    total++;
  } else {
    int i;
    for (i = 0; i < q; i++) {
      if (a[i] == s.substr(0, 2)) {
        if (s.length() > 2)
          recurse(a, b, b[i] + s.substr(2, s.length() - 1));
        else
          recurse(a, b, b[i]);
      }
    }
  }
}
string a[36], b[36];
char x[] = {'a', 'b', 'c', 'd', 'e', 'f'};
void recur(char x[], string s) {
  if (s.length() == n) {
    recurse(a, b, s);
  } else {
    for (int i = 0; i < 6; i++) {
      s.push_back(x[i]);
      recur(x, s);
      s.pop_back();
    }
  }
}
int main() {
  cin >> n >> q;
  int i;
  for (i = 0; i < q; i++) {
    cin >> a[i] >> b[i];
  }
  recur(x, "");
  cout << total << "\n";
}
