#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i;
  cin >> n >> k;
  vector<unsigned long> IP;
  for (i = 0; i < n; ++i) {
    int a, b, c, d;
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    unsigned long p = (a << 24) | (b << 16) | (c << 8) | d;
    IP.push_back(p);
  }
  unsigned long w;
  for (i = 31; i >= 0; --i) {
    w = 0;
    set<unsigned long> S;
    int j;
    for (j = 31; j >= i; --j) w |= 1 << j;
    for (j = 0; j < n; ++j) S.insert(IP[j] & w);
    if (S.size() == k) break;
  }
  if (w & 0xff == 0xff)
    printf("-1");
  else
    printf("%d.%d.%d.%d", (w >> 24) & 0xff, (w >> 16) & 0xff, (w >> 8) & 0xff,
           w & 0xff);
  return 0;
}
