#include <bits/stdc++.h>
using namespace std;
set<long long> b;
long long a[100005], x, y;
long long k, z;
int i, j;
char ch;
int main() {
  int q, n;
  scanf("%d%d", &n, &q);
  for (i = 1; i <= n; i++) {
    scanf("%lld%c%lld%c%lld%c%lld", &x, &ch, &y, &ch, &z, &ch, &k);
    a[i] = (x << 24) | (y << 16) | (z << 8) | k;
  }
  x = 0;
  for (i = 31; i >= 0; i--) {
    b.clear();
    x += 1 << i;
    for (j = 1; j <= n; j++) {
      y = x & a[j];
      b.insert(y);
    }
    if (b.size() == q) {
      y = 0;
      for (j = 31; j >= 24; j--)
        if (x & 1 << j) y |= 1 << (j % 8);
      printf("%lld.", y);
      y = 0;
      for (j = 23; j >= 16; j--)
        if (x & 1 << j) y |= 1 << (j % 8);
      printf("%lld.", y);
      y = 0;
      for (j = 15; j >= 8; j--)
        if (x & 1 << j) y |= 1 << (j % 8);
      printf("%lld.", y);
      y = 0;
      for (j = 7; j >= 0; j--)
        if (x & 1 << j) y |= 1 << (j % 8);
      printf("%lld", y);
      printf("\n");
      return 0;
    }
  }
  printf("-1\n");
}
