#include <bits/stdc++.h>
using namespace std;
const int maxn = 100006;
long long n, m, a[maxn], b[maxn];
void print(long long k) {
  printf("%I64d.%I64d.%I64d.%I64d", k >> 24, (k >> 16) % (1 << 8),
         (k >> 8) % (1 << 8), k % (1 << 8));
}
int main() {
  scanf("%I64d%I64d", &n, &m);
  for (int i = 0; i < n; ++i) {
    long long p, q, t, v;
    scanf("%I64d.%I64d.%I64d.%I64d", &p, &q, &t, &v);
    a[i] = (p << 24) + (q << 16) + (t << 8) + v;
  }
  for (int i = 1; i <= 32; ++i) {
    long long k = ((1LL << i) - 1) << (32 - i);
    for (int j = 0; j < n; ++j) b[j] = k & a[j];
    sort(b, b + n);
    int t = unique(b, b + n) - b;
    if (t == m) {
      print(k);
      return 0;
    }
  }
  printf("-1");
  ++n;
}
