#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q, ans = 0;
  cin >> n >> q;
  map<string, char> two;
  for (int i = 0; i < q; i++) {
    string s1;
    char c;
    cin >> s1 >> c;
    two[s1] = c;
  }
  string cur(n, 'a'), end(n, 'f');
  while (cur <= end) {
    string cuc = cur;
    while (cuc.length() >= 2 && two.find(cuc.substr(0, 2)) != two.end()) {
      cuc = two[cuc.substr(0, 2)] + cuc.substr(2);
    }
    if (cuc == "a") {
      ans++;
    }
    if (cur == end) break;
    int i = n - 1;
    while (cur[i] == 'f') i--;
    cur[i]++;
    for (size_t j = i + 1; j < cur.length(); j++) cur[j] = 'a';
  }
  cout << ans;
  return 0;
}
