#include <bits/stdc++.h>
using namespace std;
map<char, vector<string> > m;
string s;
char ch;
int len, q, i, ans;
void solve(string str, int l) {
  if (l > len) return;
  if (l == len) {
    ++ans;
    return;
  }
  char temp = str[0];
  for (auto it : m[temp]) solve(it, l + 1);
}
int main() {
  cin >> len >> q;
  for (i = 0; i < q; i++) {
    cin >> s >> ch;
    m[ch].push_back(s);
  }
  for (auto it : m['a']) solve(it, 2);
  cout << ans;
}
