#include <bits/stdc++.h>
using namespace std;
int n, k;
long long z[100010];
int main() {
  scanf("%d%d", &n, &k);
  for (int a = 1; a <= n; a++) {
    int v1, v2, v3, v4;
    scanf("%d.%d.%d.%d", &v1, &v2, &v3, &v4);
    z[a] = ((long long)v1 << 24) + ((long long)v2 << 16) +
           ((long long)v3 << 8) + (long long)v4;
  }
  long long ans = 1ll << 31;
  for (int a = 30; a >= 0; a--) {
    set<long long> se;
    for (int b = 1; b <= n; b++) se.insert(ans & z[b]);
    if ((int)se.size() == k) {
      printf("%d.%d.%d.%d\n", (int)(ans >> 24), (int)((ans >> 16) & 255),
             (int)((ans >> 8) & 255), (int)(ans & 255));
      return 0;
    }
    ans |= 1ll << a;
  }
  printf("-1\n");
  return 0;
}
