#include <bits/stdc++.h>
using namespace std;
map<char, vector<string> > m;
int l, n;
long long ans = 0;
void rek(int d, char last) {
  if (d == l)
    ans++;
  else
    for (int i = 0; i < m[last].size(); i++) {
      string c = m[last][i];
      rek(d + 1, c[0]);
    }
  return;
}
int main() {
  cin >> l >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    char c;
    cin >> c;
    m[c].push_back(s);
  }
  rek(1, 'a');
  cout << ans;
  return 0;
}
