#include <bits/stdc++.h>
using namespace std;
long long int n, m, cont = 0;
char a[50], b[50], c[50];
long long int rec(char a[], char b[], char c[], char d, int n) {
  if (n == 0) return 0;
  for (int i = 0; i < m; i++) {
    if (c[i] == d) {
      if (n == 1) cont++;
      rec(a, b, c, a[i], n - 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  if (n == 2) {
    for (int i = 0; i < m; i++)
      if (c[i] == 'a') cont++;
  }
  for (int i = 0; i < m; i++) {
    if (c[i] == 'a') rec(a, b, c, a[i], n - 2);
  }
  cout << cont;
  return 0;
}
