#include <bits/stdc++.h>
int masks[32] = {-2147483648, -1073741824, -536870912, -268435456, -134217728,
                 -67108864,   -33554432,   -16777216,  -8388608,   -4194304,
                 -2097152,    -1048576,    -524288,    -262144,    -131072,
                 -65536,      -32768,      -16384,     -8192,      -4096,
                 -2048,       -1024,       -512,       -256,       -128,
                 -64,         -32,         -16,        -8,         -4,
                 -2,          -1};
int main() {
  int n, k;
  std::cin >> n >> k;
  int address[4];
  int addr, mask;
  std::vector<int> addresses(n);
  char dot;
  std::vector<int> temp(k + 1);
  int c;
  for (int i = 0; i < n; i++) {
    std::cin >> address[0] >> dot >> address[1] >> dot >> address[2] >> dot >>
        address[3];
    addr = address[0] << 24 | address[1] << 16 | address[2] << 8 | address[3];
    addresses[i] = addr;
  }
  std::sort(addresses.begin(), addresses.end());
  for (int i = 0; i < 32; i++) {
    c = 0;
    for (int j = 0; j < n; j++) {
      addr = addresses[j] & masks[i];
      if (!std::binary_search(temp.begin(), temp.begin() + c, addr)) {
        temp[c] = addr;
        c++;
      }
      if (c > k) break;
      if (n - 1 - j < k - c) break;
    }
    if (c == k) {
      std::cout << ((masks[i] & 0xFF000000) >> 24) << "."
                << ((masks[i] & 0x00FF0000) >> 16) << "."
                << ((masks[i] & 0x0000FF00) >> 8) << "."
                << (masks[i] & 0x000000FF);
      return 0;
    }
  }
  std::cout << -1;
  return 0;
}
