#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, t;
  cin >> n >> t;
  string s;
  char c;
  vector<string> v[6];
  for (long long j = 0; j < t; j++) {
    cin >> s >> c;
    v[int(c) - 97].push_back(s);
  }
  long long k = 0;
  queue<pair<string, long long> > q;
  for (long long j = 0; j < v[0].size(); j++) {
    q.push(make_pair(v[0][j], 1));
  }
  while (!q.empty()) {
    pair<string, long long> h = q.front();
    if (h.second == n - 1) {
      k++;
      q.pop();
      continue;
    }
    for (long long j = 0; j < v[int(h.first[0]) - 97].size(); j++) {
      q.push(make_pair(v[int(h.first[0]) - 97][j], h.second + 1));
    }
    q.pop();
  }
  cout << k << endl;
  return 0;
}
