#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 0;
deque<char> q;
map<string, char> mp;
void gen(int idx) {
  if (idx == n) {
    deque<char> aux;
    string res;
    while (!q.empty()) {
      res += q.front();
      aux.push_back(q.front());
      q.pop_front();
    }
    bool flag = true;
    while (flag && aux.size() > 1) {
      string si;
      si += aux.front();
      aux.pop_front();
      si += aux.front();
      aux.pop_front();
      if (mp.find(si) == mp.end()) {
        flag = false;
      } else {
        aux.push_front(mp[si]);
      }
    }
    if (flag) {
      char chk = aux.front();
      if (chk == 'a') ans++;
    }
    for (char ch : res) q.push_back(ch);
  } else {
    for (char c = 'a'; c <= 'f'; c++) {
      q.push_back(c);
      gen(idx + 1);
      q.pop_back();
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  while (m--) {
    string si;
    char ci;
    cin >> si >> ci;
    mp[si] = ci;
  }
  gen(0);
  cout << ans << endl;
}
