#include <bits/stdc++.h>
long n, z, Q[100000];
long netNb(long m) {
  std::set<long> S;
  for (long i = n; S.size() <= z && i; S.insert(m & Q[--i]))
    ;
  return S.size();
}
int main(void) {
  long l, m, r, i, j;
  for (scanf("%ld%ld", &n, &z), i = n; i; Q[i] |= l << 24 | m << 16 | r << 8)
    scanf("%ld.%ld.%ld.%ld", &l, &m, &r, --i + Q);
  for (l = 0, r = 32; m = l + r >> 1, l ^ m;)
    if (z > (j = netNb(-1 << m)))
      r = m;
    else
      i = j, l = m;
  m = -1 << m, printf(l && z == i ? "%ld.%ld.%ld.%ld" : "-1", 255 & m >> 24,
                      255 & m >> 16, 255 & m >> 8, 255 & m);
  return 0;
}
