#include <bits/stdc++.h>
using namespace std;
unsigned int ip[100009];
void print(unsigned n) {
  for (int i = 0; i < 4; i++) {
    if (i) printf(".");
    printf("%u", (n << (8 * i)) >> 24);
  }
  printf("\n");
}
int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 4; j++) {
      unsigned int v;
      scanf("%u.", &v);
      ip[i] |= v << (24 - j * 8);
    }
  }
  bool solve = false;
  unsigned int ans = 0;
  for (int i = 31; i >= 0; i--) {
    ans |= 1u << i;
    set<int> net;
    for (int j = 0; j < N; j++) net.insert(ip[j] & ans);
    if (net.size() == K) {
      solve = true;
      break;
    }
  }
  if (!solve)
    puts("-1");
  else
    print(ans);
  return 0;
}
