#include <bits/stdc++.h>
unsigned int mask, temp = (1ul << 31), addr[100009];
unsigned int i, j, k, n;
std::unordered_set<unsigned int> st;
unsigned char* ma = (unsigned char*)(&mask);
int main() {
  scanf("%u%u", &n, &k);
  for (i = 0; i < n; ++i) {
    scanf("%u.%u.%u.%u", addr + 100005, addr + 100004, addr + 100003,
          addr + 100002);
    for (j = 0; j < 4; ++j) {
      ma[j] = addr[100002 + j];
    }
    addr[i] = mask;
  }
  for (mask = temp; (int)mask != -1; mask += (temp >>= 1)) {
    for (i = 0; i < n; ++i) {
      st.insert(addr[i] & mask);
    }
    if (st.size() == k) {
      printf("%u.%u.%u.%u", ma[3], ma[2], ma[1], ma[0]);
      return 0;
    }
    st.clear();
  }
  printf("-1");
}
