#include <bits/stdc++.h>
using namespace std;
long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
long long ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1},
          ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
long long gcd(long long a, long long b) {
  if (!a) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long dp[300][300];
void test_case() {
  long long n, q;
  cin >> n >> q;
  vector<pair<string, char> > arr;
  for (long long i = 0; i < q; i++) {
    string a;
    char b;
    cin >> a >> b;
    arr.push_back(make_pair(a, b));
    dp[2][b - 'a']++;
  }
  for (long long i = 3; i <= n; i++) {
    for (long long j = 0; j < q; j++) {
      long long end = arr[j].second - 'a';
      string start = arr[j].first;
      long long a = start[0] - 'a';
      long long b = start[1] - 'a';
      dp[i][end] += dp[i - 1][a];
    }
  }
  cout << dp[n][0] << "\n";
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  long long t = 1;
  while (t--) {
    test_case();
  }
}
