#include <bits/stdc++.h>
using namespace std;
int a[100000];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    int t[4];
    scanf("%d.%d.%d.%d", t, t + 1, t + 2, t + 3);
    a[i] = (t[0] << 24) | (t[1] << 16) | (t[2] << 8) | t[3];
  }
  sort(a, a + n);
  int m = -1;
  unsigned int w = -1;
  while (m <<= 1) {
    int j = 0;
    for (int i = 0; i < n; i++) {
      a[i] &= m;
      if (a[i] != a[j]) a[++j] = a[i];
    }
    j++;
    if (j < k) break;
    n = j;
    w = m;
  }
  if (n == k)
    printf("%d.%d.%d.%d\n", w >> 24, (w >> 16) & 255, (w >> 8) & 255, w & 255);
  else
    printf("-1\n");
  return 0;
}
