#include <bits/stdc++.h>
using namespace std;
void FastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
}
long long max(long long a, long long b) {
  if (a > b)
    return a;
  else
    return b;
}
long long min(long long a, long long b) {
  if (a < b)
    return a;
  else
    return b;
}
long long pow(long long B, long long P) {
  long long S = 1;
  for (long long i = 1; i <= P; i++) S = S * B;
  return S;
}
int fx4[] = {1, -1, 0, 0};
int fy4[] = {0, 0, 1, -1};
map<string, vector<string> > graph;
map<string, vector<string> > G;
map<string, int> visited;
string process(string U, string V, int I) {
  int len = U.length();
  string X;
  for (int i = 0; i < len; i++) {
    if (i == I)
      X += V;
    else
      X += U[i];
  }
  return X;
}
void BFS(string source, int N) {
  visited[source] = 1;
  queue<string> Q;
  Q.push(source);
  while (!Q.empty()) {
    string U = Q.front();
    Q.pop();
    string temp;
    temp += U[0];
    for (int j = 0; j < graph[temp].size(); j++) {
      string Y;
      Y = process(U, graph[temp][j], 0);
      if (Y.length() > N) return;
      if (visited[Y] == 0) {
        visited[Y] = 1;
        G[U].push_back(Y);
        Q.push(Y);
      }
    }
  }
}
map<string, int> L;
int BFS1(string source, int N) {
  map<string, int> Vii;
  Vii[source] = 1;
  L[source] = 0;
  queue<string> Q;
  Q.push(source);
  int Count = 0;
  while (!Q.empty()) {
    string U = Q.front();
    Q.pop();
    for (int i = 0; i < G[U].size(); i++) {
      string V = G[U][i];
      if (Vii[V] == 0) {
        Vii[V] = 1;
        Q.push(V);
        L[V] = L[U] + 1;
        if (V.length() > N || L[V] >= N) return 0;
        if (V.length() == N) Count++;
      }
    }
  }
  return Count;
}
int main() {
  FastIO();
  int N, M;
  cin >> N >> M;
  string S[10000], Y[10000];
  for (int i = 1; i <= M; i++) {
    cin >> S[i] >> Y[i];
    graph[Y[i]].push_back(S[i]);
  }
  BFS("a", N);
  int ans = BFS1("a", N);
  cout << ans << endl;
  return 0;
}
