#include <bits/stdc++.h>
using namespace std;
long long n, k, q, start;
int r1, r2, r3, r4;
bool found;
vector<int> V[111111], fina;
string A[111111];
map<int, bool> MAP;
int main() {
  cin >> n >> k;
  int n1, n2, n3, n4;
  n1 = n2 = n3 = n4 = 255;
  for (int i = (0); i < (n); i++) {
    cin >> A[i];
    stringstream aa(A[i]);
    int w, x, y, z;
    char ch;
    aa >> w >> ch >> x >> ch >> y >> ch >> z;
    V[i].push_back(w);
    V[i].push_back(x);
    V[i].push_back(y);
    V[i].push_back(z);
  }
  for (int i = (32) - 1; i >= (0); i--) {
    if (i == 0) continue;
    if (i <= 7) n1 -= (1 << (7 - i));
    if (i > 7 && i <= 15) n2 -= (1 << (15 - i));
    if (i > 15 && i <= 23) n3 -= (1 << (23 - i));
    if (i > 23 && i <= 31) n4 -= (1 << (31 - i));
    MAP.clear();
    for (int j = (0); j < (n); j++) {
      int a1 = n1 & V[j][0];
      int a2 = n2 & V[j][1];
      int a3 = n3 & V[j][2];
      int a4 = n4 & V[j][3];
      long long co = a1 * (256 * 256 * 256) + a2 * (256 * 256) + a3 * 256 + a4;
      MAP[co] = true;
    }
    if (MAP.size() == k) {
      r1 = n1;
      r2 = n2;
      r3 = n3;
      r4 = n4;
      found = true;
    }
  }
  if (found) {
    cout << r1 << "." << r2 << "." << r3 << "." << r4 << endl;
  } else
    cout << -1 << endl;
  return 0;
}
