#include <bits/stdc++.h>
using namespace std;
struct op {
  string a, b;
  int i;
};
long long int dfs(int v, vector<vector<op> > &adj, int m, int n,
                  vector<op> &q) {
  long long int count = 0;
  if (m == n - 1 && q[v].b == "a") return 1;
  if (m == n - 1) return 0;
  for (int i = 0; i < adj[v].size(); i++)
    count += dfs(adj[v][i].i, adj, m + 1, n, q);
  return count;
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<op> v(q + 1);
  for (int i = 1; i <= q; i++) {
    cin >> v[i].a >> v[i].b;
    v[i].i = i;
  }
  vector<vector<op> > adj(q + 1);
  for (int i = 1; i <= q; i++) {
    for (int j = 1; j <= q; j++)
      if (v[j].a[0] == v[i].b[0]) adj[i].push_back(v[j]);
  }
  long long int count = 0;
  for (int i = 1; i <= q; i++) {
    count += dfs(i, adj, 1, n, v);
  }
  cout << count << endl;
  return 0;
}
