#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long K = 6;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, Q;
  cin >> N >> Q;
  vector<vector<long long>> v(K);
  for (long long i = 0; i < Q; i++) {
    string s, t;
    cin >> s >> t;
    v[t[0] - 'a'].push_back(s[0] - 'a');
  }
  vector<vector<long long>> dp(N + 1, vector<long long>(K));
  dp[1][0] = 1;
  for (long long i = 1; i < N; i++)
    for (long long j = 0; j < K; j++) {
      for (long long x : v[j]) {
        dp[i + 1][x] += dp[i][j];
      }
    }
  cout << accumulate(dp[N].begin(), dp[N].end(), 0LL) << endl;
  return 0;
}
