#include <bits/stdc++.h>
using namespace std;
map<long long, int> mp;
int n, m;
long long A[100010], f[50];
int bit[100010];
int main() {
  int i, j, k;
  f[0] = 1;
  for (i = 1; i <= 35; i++) {
    f[i] = f[i - 1] * 2;
  }
  while (scanf("%d%d", &n, &m) != EOF) {
    for (i = 1; i <= n; i++) {
      int a, b, c, d;
      scanf("%d.%d.%d.%d", &a, &b, &c, &d);
      A[i] = a * f[24] + b * f[16] + c * f[8] + d;
    }
    int res = -1;
    for (i = 31; i >= 1; i--) {
      mp.clear();
      for (j = 1; j <= n; j++) {
        long long tmp = A[j] & (f[32] - f[i]);
        mp[tmp] = 1;
      }
      if (mp.size() == m) {
        res = i;
        break;
      }
    }
    if (res == -1)
      printf("-1\n");
    else {
      long long tmp = 0;
      memset(bit, 0, sizeof(bit));
      for (i = 31; i >= res; i--) {
        bit[i] = 1;
      }
      for (i = 1; i <= 4; i++) {
        if (i > 1) printf(".");
        k = 0;
        for (j = (5 - i) * 8 - 1; j >= (4 - i) * 8; j--) {
          k = k * 2 + bit[j];
        }
        printf("%d", k);
      }
      printf("\n");
    }
  }
  return 0;
}
