#include <bits/stdc++.h>
using namespace std;
long long M = 1e15 + 37;
const long long N = 1000 + 10;
const long long IM = 1e18;
const long long PI = 3.14159265;
void PV(vector<long long> v) {
  for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << "\n";
}
void PA(long long v[], long long n, long long x = 0) {
  for (long long i = x; i < n + x; i++) cout << v[i] << " ";
  cout << "\n";
}
void IN(long long a[], long long n, long long x = 0) {
  for (long long i = x; i < n + x; i++) cin >> a[i];
}
inline void PTN(long long n = 1, long long a = 0, long long b = 0,
                long long c = 0) {
  if (n--) cout << a << " ";
  if (n--) cout << b << " ";
  if (n--) cout << c << " ";
  cout << "\n";
}
inline void op() {}
long long ans = 0;
long long n, q;
vector<pair<string, char>> v;
map<string, long long> m;
void rec(string s, long long l) {
  if (n == l) {
    if (!m[s]) {
      ans++;
    }
    m[s]++;
    return;
  }
  string ss = "";
  {
    char c = s[0];
    for (long long j = 0; j < q; j++) {
      if (c == v[j].second) {
        string s1 = "";
        s1 += v[j].first;
        for (long long k = 0 + 1; k < l; k++) s1 += s[k];
        rec(s1, l + 1);
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  op();
  long long i, j, x, y, u, l;
  long long tt = 1;
  {
    i = j = x = y = u = l = 0;
    cin >> n >> q;
    string s;
    char c;
    for (i = 0; i < q; i++) {
      cin >> s >> c;
      v.push_back({s, c});
    }
    s = "a";
    ans = 0;
    rec(s, 1);
    cout << ans << "\n";
  }
  return 0;
}
