#include <bits/stdc++.h>
using namespace std;
unsigned int z[100005], sub;
set<unsigned int> mys;
int main() {
  int i, j, tst, cas = 1, n, k, bin = (1 << 8) - 1;
  int a, b, c, d;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) {
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    z[i] = a;
    z[i] <<= 8;
    z[i] += b;
    z[i] <<= 8;
    z[i] += c;
    z[i] <<= 8;
    z[i] += d;
  }
  sub = 0;
  for (i = 31; i > 0; i--) {
    sub += (1 << i);
    mys.clear();
    for (j = 0; j < n; j++) {
      mys.insert(sub & z[j]);
    }
    if (mys.size() == k) {
      break;
    }
  }
  if (i == 0) {
    puts("-1");
  } else {
    d = sub & bin;
    sub >>= 8;
    c = sub & bin;
    sub >>= 8;
    b = sub & bin;
    sub >>= 8;
    printf("%d.%d.%d.%u\n", sub, b, c, d);
  }
  return 0;
}
