#include <bits/stdc++.h>
using namespace std;
void print(unsigned int a) {
  cout << (a >> 24) << ".";
  a -= (a >> 24) << 24;
  cout << (a >> 16) << ".";
  a -= (a >> 16) << 16;
  cout << (a >> 8) << ".";
  a -= (a >> 8) << 8;
  cout << a;
}
int main() {
  int n, k;
  unsigned int a[100000];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    char temp;
    unsigned int x;
    cin >> x;
    a[i] = x << 24;
    cin >> temp;
    cin >> x;
    a[i] += x << 16;
    cin >> temp;
    cin >> x;
    a[i] += x << 8;
    cin >> temp;
    cin >> x;
    a[i] += x;
  }
  unsigned int x = 0;
  for (int i = 0; i < 32; i++) {
    map<int, bool> e;
    x += 1 << (31 - i);
    for (int j = 0; j < n; j++) {
      e[a[j] & x] = 1;
    }
    if (e.size() == k) {
      print(x);
      return 0;
    }
  }
  cout << "-1";
  return 0;
}
