#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
const int oo = 1e9;
struct data {
  int l, r;
  long long d;
};
long long cnt[MAXN + 1];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  unsigned *arr = new unsigned[n];
  for (int i = 0; i < n; ++i) {
    char s[20];
    scanf("%s", s);
    unsigned val = 0, d = 0;
    for (int j = 0; j < strlen(s); ++j) {
      if (s[j] == '.') {
        val = val * 256 + d;
        d = 0;
      } else {
        d = d * 10 + s[j] - '0';
      }
    }
    val = val * 256 + d;
    arr[i] = val;
  }
  sort(arr, arr + n);
  for (unsigned mask = 1 << 31; mask != (unsigned)-1; mask |= mask >> 1) {
    int q = 1;
    for (int i = 1; i < n; ++i) {
      if ((arr[i] & mask) != (arr[i - 1] & mask)) {
        ++q;
      }
    }
    if (q == k) {
      printf("%d.%d.%d.%d\n", mask >> 24, (mask >> 16) & 255, (mask >> 8) & 255,
             mask & 255);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
