#include <bits/stdc++.h>
using namespace std;
int n, k;
unsigned int ip[100005];
void load() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a1, a2, a3, a4;
    scanf("%d.%d.%d.%d", &a1, &a2, &a3, &a4);
    ip[i] = (((a1 << 8) + a2) << 8) + a3;
    ip[i] = (ip[i] << 8) + a4;
  }
}
void solve() {
  for (int i = 1; i < 32; i++) {
    unsigned int mask = 0;
    for (int j = 31; j > 31 - i; j--) {
      mask |= (1ull << j);
    }
    int d = 0;
    set<int> was;
    for (int j = 0; j < n; j++) {
      int t = mask & ip[j];
      if (was.count(t) == 0) d++;
      was.insert(t);
    }
    if (d == k) {
      int a4 = mask & 255, a3 = (mask >> 8) & 255, a2 = (mask >> 16) & 255,
          a1 = (mask >> 24) & 255;
      printf("%d.%d.%d.%d\n", a1, a2, a3, a4);
      return;
    }
  }
  printf("-1\n");
}
int main() {
  load();
  solve();
  return 0;
}
