#include <bits/stdc++.h>
using namespace std;
const int M = 1e3 + 10;
int str[10], sum, n, hash_[100];
int str1[10];
int check() {
  for (int j = 1; j <= n; j++) str1[j] = str[j];
  int i = 2;
  while (i <= n) {
    int num = str1[i - 1] * 10 + str1[i];
    str1[i] = hash_[num];
    if (str1[i] == 0) return 0;
    i++;
  }
  if (str1[i - 1] == 1) return 1;
  return 0;
}
void dfs(int ans) {
  if (ans == n + 1) {
    if (check()) sum++;
    return;
  }
  for (int i = 1; i <= 6; i++) {
    str[ans] = i;
    dfs(ans + 1);
  }
}
int main() {
  int m, i;
  char op1, op2, op3;
  while (~scanf("%d%d", &n, &m)) {
    memset(hash_, 0, sizeof(hash_));
    memset(str, 0, sizeof(str));
    getchar();
    for (i = 1; i <= m; i++) {
      scanf("%c%c %c", &op1, &op2, &op3);
      getchar();
      int x = (op1 - 'a' + 1) * 10 + op2 - 'a' + 1;
      hash_[x] = op3 - 'a' + 1;
    }
    sum = 0;
    dfs(1);
    printf("%d\n", sum);
  }
  return 0;
}
