#include <bits/stdc++.h>
using namespace std;
unsigned int ip[100005];
unsigned int const one = -1;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    unsigned int a, b, c, d;
    scanf("%u.%u.%u.%u", &a, &b, &c, &d);
    ip[i] = (a << 24) + (b << 16) + (c << 8) + d;
  }
  unsigned int mask = 0;
  set<unsigned int> S;
  for (int i = 1; i < 32; i++) {
    mask = one << (32 - i);
    for (int j = 0; j < n; j++) {
      S.insert(mask & ip[j]);
    }
    if (S.size() == k) {
      unsigned int a, b, c, d;
      a = mask >> 24;
      b = (mask << 8) >> 24;
      c = (mask << 16) >> 24;
      d = (mask << 24) >> 24;
      printf("%u.%u.%u.%u", a, b, c, d);
      return 0;
    }
    S.clear();
  }
  cout << -1;
  return 0;
}
