#include <bits/stdc++.h>
using namespace std;
unsigned int mas[100100];
int main() {
  int i, j, k, m, n;
  scanf("%d %d", &n, &k);
  for (i = 0; i < n; i++) {
    unsigned int a, b, c, d;
    scanf("%u.%u.%u.%u", &a, &b, &c, &d);
    mas[i] = (a << 24) + (b << 16) + (c << 8) + d;
  }
  unsigned int mask = 1u << 31;
  while (!(mask & 1)) {
    set<unsigned int> nets;
    for (i = 0; i < n; i++) {
      nets.insert(mas[i] & mask);
    }
    if (nets.size() == k) break;
    mask >>= 1u;
    mask |= 1u << 31;
  }
  if (mask & 1)
    printf("-1");
  else
    printf("%u.%u.%u.%u", mask >> 24, (mask >> 16) & 0xff, (mask >> 8) & 0xff,
           mask & 0xff);
  return 0;
}
