#include <bits/stdc++.h>
using namespace std;
long long n, k, a1, a2, a3, a4, i, j, b = 1, C[100005], t, a[35], D[100005], m,
                                      ans, r;
char ch;
int main() {
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> a1 >> ch >> a2 >> ch >> a3 >> ch >> a4;
    C[i] += a4 * b;
    b *= 256;
    C[i] += a3 * b;
    b *= 256;
    C[i] += a2 * b;
    b *= 256;
    C[i] += a1 * b;
    b = 1;
  }
  b = 2;
  for (i = 1; i <= 31; i++) {
    a[i] = b;
    b *= 2;
  }
  for (i = 30; i >= 1; i--) {
    a[i] += a[i + 1];
  }
  for (i = 31; i >= 1; i--) {
    for (j = 1; j <= n; j++) D[j] = a[i] & C[j];
    r = 1;
    sort(D + 1, D + n + 1);
    for (j = 2; j <= n; j++)
      if (D[j] != D[j - 1]) r++;
    if (r == k) {
      ans = true;
      break;
    }
  }
  if (ans == false) {
    cout << "-1" << endl;
    return 0;
  }
  cout << ((a[i] / 256) / 256) / 256 << "." << (a[i] / (256 * 256)) % 256 << "."
       << (a[i] / 256) % 256 << "." << a[i] % 256 << endl;
}
