#include <bits/stdc++.h>
using namespace std;
vector<unsigned int> v;
set<int> used;
unsigned int get(unsigned int a, unsigned int b, unsigned int c,
                 unsigned int d) {
  return a * (1 << 24) + b * (1 << 16) + c * (1 << 8) + d;
}
void pr(unsigned int x) {
  cout << ((x >> 24) & ((1 << 8) - 1)) << "." << ((x >> 16) & ((1 << 8) - 1))
       << "." << ((x >> 8) & ((1 << 8) - 1)) << "." << (x & ((1 << 8) - 1));
}
int main() {
  unsigned int i, n, k, x, a, b, c, d, ip = 0, tot, j;
  cin >> n >> k;
  for (i = 0; i < n; ++i) {
    scanf("%u.%u.%u.%u", &a, &b, &c, &d);
    v.push_back(get(a, b, c, d));
  }
  for (int h = 31; h > -1; --h) {
    ip += (1 << h);
    tot = 0;
    used.clear();
    for (j = 0; j < v.size(); ++j) used.insert(ip & v[j]);
    if (used.size() == k) {
      pr(ip);
      return 0;
    }
  }
  cout << -1;
  return 0;
}
