#include <bits/stdc++.h>
using namespace std;
struct $ {
  $() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  }
} $;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &V) {
  os << "[ ";
  for (int i = 0; i < V.size(); i++) os << (i ? ", " : "") << V[i];
  os << " ]";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &S) {
  os << "{ ";
  for (auto it = (S).begin(); it != (S).end(); it++)
    os << ((it == (S).begin()) ? "" : ", ") << (*it);
  os << " }";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const multiset<T> &MS) {
  os << "{ ";
  for (auto it = (MS).begin(); it != (MS).end(); it++)
    os << ((it == (MS).begin()) ? "" : ", ") << (*it);
  os << " }";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &P) {
  os << "(" << P.first << ", " << P.second << ")";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &MP) {
  os << "{ ";
  for (auto it = (MP).begin(); it != (MP).end(); it++)
    os << ((it == (MP).begin()) ? "" : ", ") << (it->first) << "->"
       << (it->second);
  os << " }";
  return os;
}
vector<string> v;
string s1, s2;
int n, q;
map<string, string> m;
void generate(int idx, string &s) {
  if (idx == n) {
    v.push_back(s);
  } else {
    for (char c = 'a'; c < 'g'; c++) {
      string t1 = s + c;
      generate(idx + 1, t1);
    }
  }
}
bool isPossible(string s) {
  if (s.length() == 1) {
    return s[0] == 'a';
  }
  bool ok = false;
  for (int i = 0; i < 1; i++) {
    string temp = s.substr(i, 2);
    if (m.find(temp) != m.end()) {
      string t1;
      if (i > 0) t1 += s.substr(0, i);
      t1 += m[temp];
      if (i + 2 < n) t1 += s.substr(i + 2, s.length() - i - 2);
      ok |= isPossible(t1);
    }
    if (ok) {
      return ok;
    }
  }
  return ok;
}
int main() {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    cin >> s1 >> s2;
    m[s1] = s2;
  }
  string init;
  generate(0, init);
  {
    cerr << "v"
         << ": " << (v) << endl;
  };
  int ans = 0;
  for (auto s : v) {
    if (isPossible(s)) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
