#include <bits/stdc++.h>
using namespace std;
map<char, vector<string>> operations;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < (m); ++i) {
    string s;
    char c;
    cin >> s >> c;
    swap(s[0], s[1]);
    operations[c].push_back(s);
  }
  set<string> used;
  queue<string> q;
  auto push = [&](const string& s) {
    if (used.insert(s).second) {
      q.push(s);
    }
  };
  push("a");
  int ans = 0;
  while (!q.empty()) {
    auto s = q.front();
    q.pop();
    if (s.size() == n) {
      ans++;
      continue;
    }
    char c = s.back();
    s += ' ';
    for (auto next : operations[c]) {
      *(s.end() - 2) = next[0];
      *(s.end() - 1) = next[1];
      push(s);
    }
  }
  cout << ans << endl;
  return 0;
}
