#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > V(7);
vector<vector<string> > S(7);
set<string> SS;
int n, m;
void DFS(int node, string T) {
  if (T.length() == n) {
    SS.insert(T);
    return;
  }
  for (int i = 0; i < S[node].size(); i++) {
    string R = S[node][i];
    string E;
    E += R[1];
    if (T.length() + 2 == n) {
      DFS(0, T + R);
    } else
      DFS(R[0] - 'a', T + E);
  }
}
int main() {
  cin >> n >> m;
  string y;
  char C;
  char A[m];
  for (int i = 0; i < m; i++) {
    cin >> y >> C;
    A[i] = C;
    S[C - 'a'].push_back(y);
  }
  for (int i = 0; i < m; i++) {
    if (A[i] == 'a') {
      DFS(A[i] - 'a', "");
    }
  }
  cout << SS.size() << endl;
  return 0;
}
