#include <bits/stdc++.h>
using namespace std;
vector<pair<string, string> > combs;
long long n;
long long re(string ini) {
  long long sum = 0;
  if (ini.size() == n) return 1;
  for (long long i = 0; i < combs.size(); i++) {
    if (combs[i].second == ini.substr(0, 1)) {
      sum += re(combs[i].first + ini.substr(1, ini.size() - 1));
    }
  }
  return sum;
}
int main() {
  long long q;
  bool flag = 0;
  cin >> n >> q;
  string a, b;
  for (long long i = 0; i < q; i++) {
    cin >> a >> b;
    combs.push_back(make_pair(a, b));
    if (b == "a") flag = 1;
  }
  if (flag)
    cout << re("a") << endl;
  else
    cout << 0 << endl;
  return 0;
}
