#include <bits/stdc++.h>
using namespace std;
const long long x = 4294967295;
int n, k;
long long a[111111], b[111111];
long long now, ans;
bool Can() {
  for (int i = 0; i != n; i++) b[i] = (a[i] & now);
  sort(b, b + n);
  int tot = 1;
  for (int i = 1; i != n; i++) tot += (b[i] != b[i - 1]);
  return (tot == k);
}
int main() {
  int a1, a2, a3, a4;
  cin >> n >> k;
  for (int i = 0; i != n; i++) {
    scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4);
    a[i] = (long long)a1 * (1 << 24) + a2 * (1 << 16) + a3 * (1 << 8) + a4;
  }
  ans = -1;
  for (int i = 31; i != -1; i--) {
    now = (x >> i) << i;
    if (Can()) {
      ans = now;
      break;
    }
  }
  if (ans == -1) {
    puts("-1");
    return 0;
  }
  a1 = ans >> 24;
  ans &= 0xFFFFFF;
  a2 = ans >> 16;
  ans &= 0xFFFF;
  a3 = ans >> 8;
  ans &= 0xFF;
  a4 = ans;
  printf("%d.%d.%d.%d\n", a1, a2, a3, a4);
  return 0;
}
