#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, q;
  cin >> n >> q;
  map<string, char> vals;
  map<char, long long> rev;
  string temp1;
  char temp2;
  vector<vector<string> > a(6);
  for (long long i = 0; i < q; i++) {
    cin >> temp1 >> temp2;
    vals[temp1] = temp2;
    rev[temp2]++;
    a[temp2 - 'a'].push_back(temp1);
  }
  long long ans = 0;
  if (n == 2)
    ans += rev['a'];
  else {
    queue<pair<char, long long> > check;
    check.push(make_pair('a', 1));
    while (!check.empty()) {
      pair<char, long long> t = check.front();
      check.pop();
      for (long long i = 0; i < a[t.first - 'a'].size(); i++) {
        if (t.second == n - 1) {
          ans++;
        } else {
          if (rev[a[t.first - 'a'][i][0]] > 0) {
            check.push(make_pair(a[t.first - 'a'][i][0], t.second + 1));
          }
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
