#include <bits/stdc++.h>
using namespace std;
unsigned IPaddress[100005];
map<int, bool> exist;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  {
    int temp;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 4; j++) {
        scanf("%d", &temp);
        IPaddress[i] = IPaddress[i] << 8 | temp;
        getchar();
      }
    }
  }
  unsigned ans = 0;
  bool ok = false;
  for (int j = 31; j >= 0; j--) {
    exist.clear();
    int count = 0;
    ans |= 1 << j;
    for (int i = 0; i < n; i++) {
      int temp = ans & IPaddress[i];
      if (!exist[temp]) {
        exist[temp] = true;
        count++;
      }
    }
    if (count == k) {
      ok = true;
      break;
    }
  }
  if (!ok)
    cout << -1 << endl;
  else {
    for (int i = 3; i >= 0; i--) {
      if (i)
        cout << ((ans >> i * 8) & ((1 << 8) - 1)) << ".";
      else
        cout << (ans & ((1 << 8) - 1)) << endl;
    }
  }
}
