#include <bits/stdc++.h>
using namespace std;
int count_n = 0, n, b;
void tree(char x, int l, string a[], char z[]) {
  if (l == n) {
    count_n++;
    return;
  } else {
    for (int i = 0; i < b; i++) {
      if (z[i] == x) {
        tree(a[i][0], l + 1, a, z);
      }
    }
  }
}
int main() {
  cin >> n >> b;
  string a[b];
  char z[b];
  for (int i = 0; i < b; i++) {
    cin >> a[i];
    cin >> z[i];
  }
  tree('a', 1, a, z);
  cout << count_n << endl;
}
