#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const long long INF64 = (long long)1e18;
const long double EPS = 1e-9;
const int NEUTRALMIN = INF;
const int NEUTRALMAX = -INF;
const int MAXN = (int)1e5;
const int P = 239017;
const int MOD = 1000000007;
int n, q;
map<string, char> gg;
string possible = "abcdef";
string aba;
set<string> gege;
bool can(string aba) {
  if ((int)aba.size() == 1) {
    if (aba[0] == 'a')
      return true;
    else
      return false;
  }
  for (map<string, char>::iterator it = gg.begin(); it != gg.end(); ++it) {
    string first = it->first;
    char second = it->second;
    if (aba[0] == first[0] && aba[1] == first[1]) {
      string to = second + aba.substr(2, (int)aba.size() - 2);
      if (can(to)) return true;
    }
  }
  return false;
}
void func(int len) {
  if (len == n) {
    if (can(aba)) gege.insert(aba);
    return;
  }
  for (int i = 0; i < (int)possible.size(); i++) {
    aba.push_back(possible[i]);
    func(len + 1);
    aba.pop_back();
  }
}
int main() {
  cin >> n >> q;
  string t;
  char c;
  for (int i = 0; i < q; i++) {
    cin >> t >> c;
    gg[t] = c;
  }
  func(0);
  cout << gege.size();
  return 0;
}
