#include <bits/stdc++.h>
using namespace std;
int n, k;
long long ip[100000];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    long long a, b, c, d;
    scanf("%I64d.%I64d.%I64d.%I64d", &a, &b, &c, &d);
    ip[i] = 0;
    ip[i] = ip[i] << 8 | a;
    ip[i] = ip[i] << 8 | b;
    ip[i] = ip[i] << 8 | c;
    ip[i] = ip[i] << 8 | d;
  }
  int res = -1;
  for (int i = 1; i < 32; ++i) {
    long long mask = ((1LL << i) - 1) << (32 - i);
    set<long long> st;
    for (int j = 0; j < n; ++j) {
      st.insert(ip[j] & mask);
    }
    if ((int)st.size() == k) {
      res = i;
      break;
    }
  }
  if (res == -1) {
    printf("-1\n");
  } else {
    long long mask = ((1LL << res) - 1) << (32 - res);
    long long d = mask & 0xFF;
    mask >>= 8;
    long long c = mask & 0xFF;
    mask >>= 8;
    long long b = mask & 0xFF;
    mask >>= 8;
    long long a = mask & 0xFF;
    mask >>= 8;
    printf("%I64d.%I64d.%I64d.%I64d\n", a, b, c, d);
  }
  return 0;
}
