#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
unsigned a[N];
int n, k;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < (n); i++) {
    int n1, n2, n3, n4;
    scanf("%d.%d.%d.%d", &n1, &n2, &n3, &n4);
    a[i] = (n1 << 24) + (n2 << 16) + (n3 << 8) + n4;
  }
  int now = 0, tp, pr;
  int cnt = 0;
  sort(a, a + n);
  now = (1 << 31);
  for (int i = 1; i < 32; i++) {
    pr = 0;
    cnt = 0;
    for (int j = 0; j < (n); j++) {
      tp = (now & a[j]);
      if (tp != pr || j == 0) {
        cnt++;
        pr = tp;
      }
    }
    if (cnt == k) {
      break;
    }
    now = (now >> 1);
  }
  if (cnt != k)
    puts("-1");
  else {
    printf("%u.%u.%u.%u\n", (now >> 24) & 255, (now >> 16) & 255,
           (now >> 8) & 255, now & 255);
  }
}
