#include <bits/stdc++.h>
using namespace std;
const int N = 400;
const int INF = 2000000000;
int i, j, n, m, k, a[N], ans;
string s[N];
char b[N];
void dfs(int k, char p) {
  if (k == n) {
    if (p == 'a') ans++;
    return;
  }
  for (int i = 0; i < m; i++)
    if (k == 0 || k > 0 && p == s[i][0]) dfs(k + 1, b[i]);
}
int main() {
  cin >> n >> m;
  n--;
  for (i = 0; i < m; i++) cin >> s[i] >> b[i];
  ans = 0;
  dfs(0, '1');
  cout << ans;
  return 0;
}
