#include <bits/stdc++.h>
using namespace std;
int N, K;
vector<unsigned int> ips;
set<unsigned int> S;
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    s += '.';
    unsigned int a = 0, b = 0, c = 0, d = 0;
    int j = 0;
    for (; s[j] != '.'; ++j) a = a * 10 + (s[j] - '0');
    for (++j; s[j] != '.'; ++j) b = b * 10 + (s[j] - '0');
    for (++j; s[j] != '.'; ++j) c = c * 10 + (s[j] - '0');
    for (++j; s[j] != '.'; ++j) d = d * 10 + (s[j] - '0');
    unsigned int v = (a << 24) + (b << 16) + (c << 8) + d;
    ips.push_back(v);
  }
  int best_i = -1;
  unsigned int inf = -1;
  for (int i = 31; i >= 1; --i) {
    unsigned int mask = inf << i;
    S.clear();
    for (int j = 0; j < N; j++) S.insert(ips[j] & mask);
    if ((int)S.size() == K) best_i = i;
    if ((int)S.size() >= K) break;
  }
  if (best_i == -1) {
    cout << "-1\n";
    return 0;
  }
  unsigned int mask = inf << best_i;
  unsigned int a = mask >> 24;
  mask &= (1u << 24) - 1;
  unsigned int b = mask >> 16;
  mask &= (1u << 16) - 1;
  unsigned int c = mask >> 8;
  mask &= (1u << 8) - 1;
  unsigned int d = mask;
  cout << a << "." << b << "." << c << "." << d << endl;
  return 0;
}
