#include <bits/stdc++.h>
using namespace std;
int n, q;
char a[36], b[36];
int recursion(char c, int s) {
  int total = 0;
  if (s == n - 1) return 1;
  for (int i = 0; i < q; i++)
    if (c == b[i]) {
      total += recursion(a[i], s + 1);
    }
  return total;
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < q; i++) cin >> a[i] >> b[i] >> b[i];
  cout << recursion('a', 0);
}
