#include <bits/stdc++.h>
using namespace std;
const int MX = 25;
int main(int argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  string a[n];
  for (int i = (0), _b = (n); i < (_b); ++i) cin >> a[i];
  uint8_t g[4];
  uint32_t b[n];
  for (int i = (0), _b = (n); i < (_b); ++i) {
    uint8_t x = 0;
    int idx = 0;
    for (int j = (0), _b = (a[i].length()); j < (_b); ++j) {
      if (a[i][j] == '.') {
        g[idx] = x;
        x = 0;
        ++idx;
      } else
        x = x * 10 + a[i][j] - '0';
    }
    g[idx] = x;
    b[i] = (g[0] << 24) | (g[1] << 16) | (g[2] << 8) | g[3];
  }
  uint32_t mask = 0xffffffff;
  uint32_t ans = 0;
  while (mask) {
    set<uint32_t> s;
    for (int i = (0), _b = (n); i < (_b); ++i) s.insert(b[i] & mask);
    if (s.size() == k) ans = mask;
    mask <<= 1;
  }
  if (!ans)
    cout << -1 << endl;
  else {
    uint8_t a0 = (ans >> 24) & 0xff;
    uint8_t a1 = (ans >> 16) & 0xff;
    uint8_t a2 = (ans >> 8) & 0xff;
    uint8_t a3 = ans & 0xff;
    printf("%d.%d.%d.%d\n", a0, a1, a2, a3);
  }
  return 0;
}
