#include <bits/stdc++.h>
const int Q = 50;
char str[Q][5];
struct dfser {
  int n, q;
  int dfs(char top_char, int length) {
    if (length == n) return 1;
    int ans = 0;
    for (int i = 1; i <= q; ++i) {
      if (top_char == str[i][2]) ans += dfs(str[i][0], length + 1);
    }
    return ans;
  }
  dfser(int ni, int qi) : n(ni), q(qi) {}
};
char next_char() {
  char c = getchar();
  for (; !(c >= 'a' && c <= 'f');) c = getchar();
  return c;
}
int main(int argc, char const *argv[]) {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= q; ++i) {
    str[i][0] = next_char();
    str[i][1] = next_char();
    str[i][2] = next_char();
  }
  printf("%d\n", dfser(n, q).dfs('a', 1));
  return 0;
}
