#include <bits/stdc++.h>
using namespace std;
const int inf = (int)2 * 1e9;
const int MOD = (int)1e9 + 7;
const int p = 239;
const int MAX_N = 10;
const long double pi = 3.1415926535897932384626;
const long double eps = 1e-6;
int n, q;
string a[36][2];
int rec(int num, string s) {
  if (num == n) {
    for (int i = 0; i < n - 1; i++)
      for (int j = 0; j < q; j++)
        if (s[0] == a[j][0][0] && s[1] == a[j][0][1]) {
          s = s.substr(2, s.size() - 2);
          s = a[j][1] + s;
          break;
        }
    if (s == "a")
      return 1;
    else
      return 0;
  } else {
    int ans = 0;
    for (int i = 0; i < 6; i++) ans += rec(num + 1, s + (char)(i + 'a'));
    return ans;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < q; i++) cin >> a[i][0] >> a[i][1];
  cout << rec(0, "");
  return 0;
}
