#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const long long INF = 1e9 + 7;
const long double EPS = 1e-9;
const long long P = 485437;
const int C = 2016;
char b[N];
int n, q, ans = 0;
string a[N];
map<string, bool> used;
void dfs(string s) {
  if (used[s]) return;
  used[s] = true;
  if (s.size() == n) {
    ans++;
    return;
  }
  string x = "", y = "", k = "";
  for (int i = 1; i <= 1; i++) {
    for (int j = 0; j < q; j++)
      if (b[j] == s[0]) {
        k = x + a[j];
        for (int c = i; c < s.size(); c++) k += s[c];
        dfs(k);
      }
    x += s[i];
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> q;
  for (int i = 0; i < q; i++) cin >> a[i] >> b[i];
  dfs("a");
  cout << ans << endl;
  return 0;
}
