#include <bits/stdc++.h>
using namespace std;

int n;
int d;
char buf[114514];
map<string, set<int> > mp; 
string names[114514];
set<int> ans[114514];

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0) return 0;

    scanf("%d", &d);
    for (int i=0; i<n; i++) {
      scanf("%s", buf);
      names[i] = buf;
      ans[i].clear();
    }
    sort(names, names+n);
    mp.clear();
    for (int i=0; i<n; i++) {
      mp[names[i]].insert(i);
    }

    if (d == 1) {
      for (int i=0; i<n; i++) {
        for (int j=0; j<names[i].size(); j++) {
          for (int k : mp[names[i].substr(0, j) + names[i].substr(j+1)]) {
            if (k != i) {
              ans[min(i, k)].insert(max(i, k));
            }
          }

          for (char k='a'; k<='z'; k++) {
            //cout << "rep: " << names[i].substr(0, j)+k+names[i].substr(j+1) << "\n";
            for (int t : mp[names[i].substr(0, j)+k+names[i].substr(j+1)]) {
              if (t != i) {
                ans[min(i, t)].insert(max(i, t));
              }
            }

            //cout << "ins: " << names[i].substr(0, j)+k+names[i].substr(j) << "\n";
            for (int t : mp[names[i].substr(0, j)+k+names[i].substr(j)]) {
              if (t != i) {
                ans[min(i, t)].insert(max(i, t));
              }
            }
          }

          for (int k=j+1; k<names[i].size(); k++) {
            string g = names[i].substr(0, j)+names[i][k]+names[i].substr(j+1, k-j-1)+names[i][j]+names[i].substr(k+1);
            //cout << "sw: " << g << "\n";
            for (int t : mp[g]) {
              if (t != i) {
                ans[min(i, t)].insert(max(i, t));
              }
            }
            break;
          }
        }

        for (char k='a'; k<='z'; k++) {
          //cout << "ins: " << names[i]+k << "\n";
          for (int t : mp[names[i]+k]) {
            if (t != i) {
              ans[min(i, t)].insert(max(i, t));
            }
          }
        }
      }
    } else if (d == 2) {
      for (int i=0; i<n; i++) {
        for (int j=0; j<names[i].size(); j++) {
          //cout << "del: " << names[i].substr(0, j) + names[i].substr(j+1) << "\n";
          mp[names[i].substr(0, j) + names[i].substr(j+1)].insert(i);
          for (char k='a'; k<='z'; k++) {
            mp[names[i].substr(0, j)+k+names[i].substr(j+1)].insert(i);
            mp[names[i].substr(0, j)+k+names[i].substr(j)].insert(i);
          }

          for (int k=j+1; k<names[i].size(); k++) {
            string g = names[i].substr(0, j)+names[i][k]+names[i].substr(j+1, k-j-1)+names[i][j]+names[i].substr(k+1);
            mp[g].insert(i);
            break;
          }
        }

        for (char k='a'; k<='z'; k++) {
          mp[names[i]+k].insert(i);
        }
      }

      for (auto itr : mp) {
        for (int i : itr.second) {
          for (int j : itr.second) {
            if (i < j) ans[i].insert(j);
          }
        } 
      }
    } else assert(0);

    int cnt = 0;
    for (int i=0; i<n; i++) {
      for (int j : ans[i]) {
        printf("%s,%s\n", names[i].c_str(), names[j].c_str());
        cnt++;
      }
    }
    printf("%d\n", cnt);
  }
}