#include <bits/stdc++.h>
using namespace std;
long long ip[110000];
set<long long> ss;
long long ans[5];
int n, m;
int main() {
  scanf("%d %d", &n, &m);
  char s[20];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      int x;
      scanf("%[^.]", s);
      x = atoi(s);
      ip[i] <<= 8;
      ip[i] += x;
      char c;
      scanf("%c", &c);
    }
    int x;
    scanf("%d", &x);
    ip[i] <<= 8;
    ip[i] += x;
  }
  long long mask = 0;
  for (int k = 1; k < 32; k++) {
    mask |= (1LL << (32 - k));
    ss.clear();
    for (int i = 0; i < n; i++) {
      ss.insert(mask & ip[i]);
    }
    int num = ss.size();
    if (num == m) {
      for (int i = 3; i >= 0; i--) {
        ans[i] = (mask & ((1 << 8) - 1));
        mask >>= 8;
      }
      for (int i = 0; i < 4; i++) {
        printf("%I64d", ans[i]);
        if (i != 3) printf(".");
      }
      printf("\n");
      return 0;
    }
  }
  printf("-1\n");
}
