#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 100;
int n, q;
map<int, vector<int> > m;
void clear() { m.clear(); }
void solve() {
  for (int i = 0; i < q; i++) {
    char from[5], to[5];
    scanf("%s", from);
    scanf("%s", to);
    m[to[0] - 'a' + 1].push_back(from[0] - 'a' + 1 + 7 * (from[1] - 'a' + 1));
  }
  set<int> s1, s2;
  s1.insert(1);
  for (int i = 0; i < n; i++) {
    set<int> &f = i & 1 ? s2 : s1;
    set<int> &t = i & 1 ? s1 : s2;
    t.clear();
    for (auto num : f) {
      int d = num % 7;
      num -= d;
      num *= 7;
      for (auto p : m[d]) t.insert(num + p);
    }
  }
  printf("%d\n", n & 1 ? s1.size() : s2.size());
}
int main() {
  while (scanf("%d%d", &n, &q) == 2) {
    clear();
    solve();
    return 0;
  }
  return 0;
}
