#include <bits/stdc++.h>
char ss[10], p_ss[10];
char a[50][5], b[50];
char st[3];
int n, m;
int ans, sum;
bool judge() {
  for (int i = 1; i < n; i++) {
    int mark = 0;
    st[0] = p_ss[i - 1], st[1] = p_ss[i], st[2] = '\0';
    for (int j = 0; j < m; j++) {
      if (strcmp(st, a[j]) == 0) {
        p_ss[i] = b[j];
        mark = 1;
      }
    }
    if (!mark) return false;
  }
  if (p_ss[n - 1] == 'a')
    return true;
  else
    return false;
}
void dfs(int index) {
  if (index == n) {
    ss[n] = '\0';
    strcpy(p_ss, ss);
    if (judge()) {
      sum++;
    }
    return;
  }
  for (int i = 0; i < 6; i++) {
    ss[index] = 'a' + i;
    dfs(index + 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%s", a[i]);
    getchar();
    scanf("%c", &b[i]);
  }
  sum = 0;
  memset(ss, '\0', sizeof(ss));
  for (int i = 0; i < 6; i++) {
    ss[0] = 'a' + i;
    dfs(1);
  }
  printf("%d\n", sum);
  return 0;
}
