#include <bits/stdc++.h>
using namespace std;
vector<int> v[500000];
map<long long, int> mp;
long long i, j, t, n, k, cur;
long long a[200000], tmp, t1, t2, t3, t4;
bool boo;
string s;
int main() {
  cin >> n >> k;
  getline(cin, s);
  for (i = 1; i <= n; i++) {
    getline(cin, s);
    j = 0;
    tmp = 0;
    while (s[j] != '.') {
      tmp *= 10;
      tmp += s[j] - '0';
      j++;
    }
    a[i] = tmp;
    j++;
    tmp = 0;
    while (s[j] != '.') {
      tmp *= 10;
      tmp += s[j] - '0';
      j++;
    }
    a[i] = a[i] << 8;
    a[i] += tmp;
    j++;
    tmp = 0;
    while (s[j] != '.') {
      tmp *= 10;
      tmp += s[j] - '0';
      j++;
    }
    a[i] = a[i] << 8;
    a[i] += tmp;
    j++;
    tmp = 0;
    while (j < s.length()) {
      tmp *= 10;
      tmp += s[j] - '0';
      j++;
    }
    a[i] = a[i] << 8;
    a[i] += tmp;
  }
  boo = false;
  tmp = 0;
  for (i = 31; i >= 0; i--) {
    tmp += (1ll << i);
    cur = 0;
    for (j = 1; j <= n; j++)
      if (mp[a[j] & tmp] != (i + 1)) {
        cur++;
        mp[a[j] & tmp] = (i + 1);
      }
    if (cur == k) {
      boo = true;
      break;
    }
  }
  if (!boo) {
    cout << -1 << endl;
    return 0;
  }
  t4 = tmp - ((tmp >> 8) << 8);
  tmp = tmp >> 8;
  t3 = tmp - ((tmp >> 8) << 8);
  tmp = tmp >> 8;
  t2 = tmp - ((tmp >> 8) << 8);
  tmp = tmp >> 8;
  t1 = tmp;
  cout << t1 << "." << t2 << "." << t3 << "." << t4 << endl;
}
