#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:33554432")
using namespace std;
int n, k;
int ip[1 << 17];
int m[1 << 17];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    int a, b, c, d;
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    ip[i] = (a << 24) | (b << 16) | (c << 8) | d;
  }
  sort(ip, ip + n);
  int mask = 0;
  for (int bit = 31; bit > 0; --bit) {
    mask |= 1 << bit;
    for (int i = 0; i < n; ++i) m[i] = ip[i] & mask;
    int cnt = 1;
    for (int i = 1; i < n; ++i)
      if (m[i] != m[i - 1]) ++cnt;
    if (cnt == k) {
      printf("%d.%d.%d.%d\n", (mask >> 24) & 255, (mask >> 16) & 255,
             (mask >> 8) & 255, mask & 255);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
