#include <bits/stdc++.h>
using namespace std;
int sc() {
  int i = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') i = i * 10 + c - '0', c = getchar();
  return i * f;
}
char h[555];
int a[555][56], s[95], ans, n, q;
bool jud(int x) {
  if (x == n) return s[x] == 0;
  for (int i = 1; i <= q; i++)
    if (a[i][1] == s[x] && a[i][2] == s[x + 1]) {
      int c2 = s[x + 1];
      s[x + 1] = a[i][3];
      if (jud(x + 1)) {
        s[x + 1] = c2;
        return 1;
      }
      s[x + 1] = c2;
    }
  return 0;
}
void dfs(int x) {
  if (x > n) {
    if (jud(1)) ans++;
    return;
  }
  for (int i = 0; i < 6; i++) s[x] = i, dfs(x + 1);
}
int main() {
  n = sc(), q = sc();
  for (int i = 1; i <= q; i++) {
    scanf("%s", h + 1);
    a[i][1] = h[1] - 'a';
    a[i][2] = h[2] - 'a';
    scanf("%s", h + 1);
    a[i][3] = h[1] - 'a';
  }
  dfs(1);
  cout << ans;
  return 0;
}
