#include <bits/stdc++.h>
using namespace std;
int const maxn = 100100;
long long a[maxn];
set<long long> s;
int main() {
  int n, k;
  long long x1, x2, x3, x4;
  while (~scanf("%d%d", &n, &k)) {
    for (int i = 0; i < n; ++i) {
      scanf("%I64d.%I64d.%I64d.%I64d", &x1, &x2, &x3, &x4);
      a[i] = (x1 << 24) | (x2 << 16) | (x3 << 8) | x4;
    }
    long long ret = -1;
    for (int i = 31; i >= 0; --i) {
      long long t = (1LL << 32) - (1LL << i);
      s.clear();
      for (int j = 0; j < n; ++j) {
        s.insert(t & a[j]);
      }
      if (s.size() == k) {
        ret = t;
        break;
      }
    }
    if (ret == -1)
      puts("-1");
    else {
      printf("%I64d.%I64d.%I64d.%I64d\n", ret >> 24, (ret >> 16) & 255,
             (ret >> 8) & 255, ret & 255);
    }
  }
  return 0;
}
