#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:500000000")
using namespace std;
long long n, i, j, k, open, a[100005], t[40], x, y;
char s[100005];
map<long long, long long> m;
int gcd(int a, int b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}
int main() {
  scanf("%I64d%I64d", &n, &k);
  for (i = 0; i < n; i++) {
    scanf("%I64d.", &x);
    for (j = 7; j >= 0; j--) t[j] = x % 2, x /= 2;
    scanf("%I64d.", &x);
    for (j = 15; j >= 8; j--) t[j] = x % 2, x /= 2;
    scanf("%I64d.", &x);
    for (j = 23; j >= 16; j--) t[j] = x % 2, x /= 2;
    scanf("%I64d", &x);
    for (j = 31; j >= 24; j--) t[j] = x % 2, x /= 2;
    x = 1;
    for (j = 31; j >= 0; j--) a[i] += x * t[j], x *= 2;
  }
  x = 1;
  for (i = 0; i < 31; i++) x *= 2;
  y = x;
  for (i = 1; i < 32; i++) {
    int cnt = 0;
    for (j = 0; j < n; j++)
      if (m[a[j] & x] != i) cnt++, m[a[j] & x] = i;
    if (cnt == k) {
      x = 1;
      k = 0;
      for (j = 7; j >= 0; j--) k += x * (j < i), x *= 2;
      printf("%I64d.", k);
      x = 1;
      k = 0;
      for (j = 15; j >= 8; j--) k += x * (j < i), x *= 2;
      printf("%I64d.", k);
      x = 1;
      k = 0;
      for (j = 23; j >= 16; j--) k += x * (j < i), x *= 2;
      printf("%I64d.", k);
      x = 1;
      k = 0;
      for (j = 31; j >= 24; j--) k += x * (j < i), x *= 2;
      printf("%I64d\n", k);
      return 0;
    }
    y /= 2;
    x += y;
  }
  printf("-1\n");
  return 0;
}
