#include <bits/stdc++.h>
using namespace std;
pair<string, string> p[40];
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
map<string, int> mp;
vector<string> vec;
int n, q;
void rec(string s) {
  if (s.size() == n) {
    vec.push_back(s);
    return;
  }
  for (int i = 0; i < 6; i++) {
    rec(s + arr[i]);
  }
}
int solve(string s) {
  if (s.size() == 1 && s != "a") return -1;
  if (s.size() == 1 && s == "a") {
    return 1;
  }
  if (mp[s] != 0) return mp[s];
  for (int i = 0; i < q; i++) {
    if (p[i].first[0] == s[0] && p[i].first[1] == s[1]) {
      string tmp = s;
      mp[s] = max(solve(tmp.replace(0, 2, p[i].second)), mp[s]);
    }
  }
  return mp[s];
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> p[i].first >> p[i].second;
  }
  rec("");
  long long ans = 0;
  for (int i = 0; i < vec.size(); i++) {
    if (solve(vec[i]) > 0) {
      ans++;
    }
  }
  cout << ans << endl;
}
