#include <bits/stdc++.h>
using namespace std;
char ch;
unsigned int a[100010] = {0}, b[34] = {0}, c[32] = {0};
void pp(unsigned int u) {
  int a1, a2, a3, a4;
  a1 = u % 256;
  u /= 256;
  a2 = u % 256;
  u /= 256;
  a3 = u % 256;
  u /= 256;
  a4 = u % 256;
  printf("%d.%d.%d.%d\n", a4, a3, a2, a1);
}
int main() {
  unsigned int n, k, a1, a2, a3, a4, t, i, j;
  set<int> s;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a1 >> ch >> a2 >> ch >> a3 >> ch >> a4;
    a[i] = ((a1 * 256 + a2) * 256 + a3) * 256 + a4;
  }
  c[0] = 1;
  for (i = 1; i < 32; i++) c[i] = c[i - 1] * 2;
  b[0] = c[31];
  for (i = 1; i < 32; i++) {
    b[i] = b[i - 1] + c[31 - i];
  }
  int flag = 1;
  for (i = 0; i < 32; i++) {
    s.clear();
    for (j = 0; j < n; j++) {
      s.insert(b[i] & a[j]);
    }
    if (s.size() == k) {
      pp(b[i]);
      flag = 0;
      break;
    }
  }
  if (flag) cout << -1 << endl;
  return 0;
}
