#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
unsigned int a[MAX_N + 3], b[MAX_N + 3];
unsigned int ans;
int n, k;
inline bool check() {
  int i;
  for (i = 0; i < n; ++i) b[i] = a[i] & ans;
  sort(b, b + n);
  int t = 1;
  for (i = 1; i < n; ++i) t += b[i] != b[i - 1];
  return t == k;
}
int main() {
  int i, j, t;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; ++i) {
    for (j = 0; j < 4; ++j) {
      scanf("%d", &t);
      getchar();
      a[i] = t + (a[i] << 8);
    }
  }
  for (i = 31; i >= 0; --i) {
    ans |= 1 << i;
    if (check()) break;
  }
  if (i >= 0) {
    for (i = 3; i >= 0; --i) {
      a[i] = ans & 0xFF;
      ans >>= 8;
    }
    for (i = 0; i < 4; ++i) {
      printf("%u%c", a[i], (i == 3 ? '\n' : '.'));
    }
  } else
    printf("-1\n");
  return 0;
}
