#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
const double dinf = 1e30;
const long long linf = 1LL << 55;
const int N = 111111;
int n, k;
unsigned ip[N];
set<unsigned> s;
void solve() {
  unsigned ipSeg[4], mask = 0;
  for (int i = 31; i >= 0; i--) {
    mask |= (1u << i);
    s.clear();
    for (int j = 0; j < n; j++) {
      s.insert(ip[j] & mask);
    }
    if (s.size() == k) {
      int mm = (1 << 8) - 1;
      for (int j = 0; j < 4; j++) {
        ipSeg[j] = mask & mm;
        mask >>= 8;
      }
      printf("%u.%u.%u.%u\n", ipSeg[3], ipSeg[2], ipSeg[1], ipSeg[0]);
      return;
    }
  }
  cout << -1 << endl;
}
int main() {
  while (cin >> n >> k) {
    for (int i = 0; i < n; i++) {
      unsigned a, b, c, d;
      scanf("%u.%u.%u.%u", &a, &b, &c, &d);
      ip[i] = (a << 24) | (b << 16) | (c << 8) | d;
    }
    solve();
  }
  return 0;
}
