#include <bits/stdc++.h>
using namespace std;
int n, m, a[1000100], b[1000100];
vector<string> v[10];
int main() {
  int r, k, i, c = 0, x = 0, y = 0, j, t, l, z, x1 = 0, y1 = 0;
  long long ans = 0;
  string p, s;
  scanf("%d %d", &n, &m);
  while (m--) {
    cin >> p >> s;
    v[s[0] - 'a'].push_back(p);
  }
  queue<string> q;
  q.push("a");
  while (!q.empty()) {
    s = q.front();
    q.pop();
    if (s.size() == n) c++;
    if (s.size() >= n) continue;
    x = s[0] - 'a';
    for (i = 0; i < v[x].size(); i++) {
      q.push(v[x][i] + s.substr(1, s.size() - 1));
    }
  }
  cout << c << endl;
  return 0;
}
