#include <bits/stdc++.h>
using namespace std;
unsigned readIP() {
  unsigned int a, b, c, d;
  scanf("%u.%u.%u.%u", &a, &b, &c, &d);
  return ((a * 256 + b) * 256 + c) * 256 + d;
}
void printIP(unsigned x) {
  int d = x % 256;
  x /= 256;
  int c = x % 256;
  x /= 256;
  int b = x % 256;
  x /= 256;
  int a = x % 256;
  printf("%d.%d.%d.%d\n", a, b, c, d);
}
unsigned a[100000];
int count(unsigned x, int n) {
  map<unsigned, int> num;
  for (int i = 0; i < n; ++i) ++num[x & a[i]];
  return num.size();
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) a[i] = readIP();
  bool flag = 0;
  for (unsigned int i = (unsigned)1 << 31, now = 0; i > 1; i /= 2) {
    now |= i;
    if (count(now, n) == k) {
      printIP(now);
      flag = 1;
      break;
    }
  }
  if (!flag) puts("-1");
  return 0;
}
