#include <bits/stdc++.h>
using namespace std;
long long int dp[7][7];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int n, q;
  cin >> n >> q;
  map<string, char> mp;
  while (q--) {
    string s;
    char p;
    cin >> s >> p;
    mp[s] = p;
  }
  dp[1][0] = 1;
  for (long long int i = 2; i <= n; ++i) {
    for (auto it : mp) {
      string s = it.first;
      dp[i][s[0] - 'a'] += dp[i - 1][it.second - 'a'];
    }
  }
  long long int ans = 0;
  for (long long int i = 0; i < 6; ++i) {
    ans += dp[n][i];
  }
  cout << ans << "\n";
  return 0;
}
