#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10;
int n, k;
unsigned a[N];
bool ok(unsigned ans) {
  map<unsigned, int> mp;
  for (int i = 1; i <= n; i++) {
    unsigned tmp = a[i] & ans;
    mp[tmp] = 1;
  }
  if (mp.size() == k) return 1;
  return 0;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int t1, t2, t3, t4;
    scanf("%d.%d.%d.%d", &t1, &t2, &t3, &t4);
    a[i] |= t1;
    a[i] <<= 8;
    a[i] |= t2;
    a[i] <<= 8;
    a[i] |= t3;
    a[i] <<= 8;
    a[i] |= t4;
  }
  unsigned ans = 0;
  for (int i = 31; i >= 0; i--) {
    ans |= 1 << i;
    if (ok(ans)) {
      printf("%d.", ans >> 24);
      ans = (ans << 8) >> 8;
      printf("%d.", ans >> 16);
      ans = (ans << 16) >> 16;
      printf("%d.", ans >> 8);
      ans = (ans << 24) >> 24;
      printf("%d\n", ans);
      return 0;
    }
  }
  puts("-1");
  return 0;
}
