#include <bits/stdc++.h>
using namespace std;
int a[6][6];
int brute(int n, int last) {
  if (n == 0) {
    return (last == 0 ? 1 : 0);
  }
  int res = 0;
  for (int i = 0; i < 6; ++i) {
    int next = a[last][i];
    if (next != -1) {
      res += brute(n - 1, next);
    }
  };
  return res;
}
int main() {
  memset(a, -1, sizeof(a));
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < q; ++i) {
    char ch1, ch2, ch3;
    scanf(" %c%c %c", &ch1, &ch2, &ch3);
    ;
    a[ch1 - 'a'][ch2 - 'a'] = ch3 - 'a';
  }
  int res = 0;
  for (int i = 0; i < 6; ++i) {
    res += brute(n - 1, i);
  }
  printf("%d\n", res);
  return 0;
}
