#include <bits/stdc++.h>
using namespace std;
int n, k;
unsigned a[100500];
set<unsigned> s;
void write(unsigned msk) {
  printf("%d.%d.%d.%d\n", msk >> 24, (msk >> 16) & 255, (msk >> 8) & 255,
         msk & 255);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    unsigned x1, x2, x3, x4;
    scanf("%d.%d.%d.%d", &x1, &x2, &x3, &x4);
    a[i] = (x1 << 24) + (x2 << 16) + (x3 << 8) + x4;
  }
  for (unsigned msk = (1 << 31); msk != -1; msk = (1 << 31) + (msk >> 1)) {
    s.clear();
    for (int i = 0; i < n; ++i) s.insert(a[i] & msk);
    if (s.size() == k) {
      write(msk);
      return 0;
    }
  }
  printf("-1\n");
}
