#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
string work(int x) {
  char ch[10];
  int t = 1, tmp = 0;
  while (x) {
    if (x & 1) tmp += t;
    t *= 10;
    x /= 2;
  }
  sprintf(ch, "%08d", tmp);
  return string(ch);
}
string cal(int val) {
  if (val > 8) val = 8;
  int ans = 0;
  char ch[10];
  for (int i = 8 - val; i < 8; i++) ans += 1 << i;
  sprintf(ch, "%d", ans);
  return string(ch);
}
string ch[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a, b, c, d;
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    ch[i].append(work(a));
    ch[i].append(work(b));
    ch[i].append(work(c));
    ch[i].append(work(d));
  }
  sort(ch, ch + n);
  for (int i = 1; i <= 32; i++) {
    int sum = 1;
    for (int j = 0; j < n; j++) {
      if (j && ch[j].substr(0, i) != ch[j - 1].substr(0, i)) sum++;
    }
    if (sum == k) {
      string tmp = "";
      sum = i;
      for (int p = 0; p < 4; p++) {
        tmp += cal(sum);
        sum -= min(8, sum);
        if (p != 3) tmp += ".";
      }
      cout << tmp << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
