#include <bits/stdc++.h>
using namespace std;
inline unsigned long int ip2long(int a, int b, int c, int d) {
  unsigned long int res = 0;
  res = a;
  res = res * 0x100 + b;
  res = res * 0x100 + c;
  res = res * 0x100 + d;
  return res;
}
int main() {
  set<unsigned long int> data;
  int n, k;
  int i;
  cin >> n;
  cin >> k;
  for (int i = 0; i < n; i++) {
    unsigned int a, b, c, d;
    char ch;
    cin >> a >> ch >> b >> ch >> c >> ch >> d;
    data.insert(ip2long(a, b, c, d));
  }
  unsigned long int mask;
  mask = 1;
  mask = mask << 31;
  unsigned long int tmpmask = mask;
  unsigned long int maxGroups = 2;
  for (i = 0; i < 31; i++) {
    set<unsigned long int> sets;
    if (k <= maxGroups) {
      for (set<unsigned long int>::iterator it = data.begin(); it != data.end();
           ++it) {
        unsigned long int tmp = ((*it) & mask);
        sets.insert(tmp);
        if (sets.size() > k) break;
      }
      if (sets.size() == k) {
        unsigned int a, b, c, d;
        d = mask % 0x100;
        mask = mask / 0x100;
        c = mask % 0x100;
        mask = mask / 0x100;
        b = mask % 0x100;
        mask = mask / 0x100;
        a = mask % 0x100;
        mask = mask / 0x100;
        cout << dec << a << "." << b << "." << c << "." << d << endl;
        return 0;
      }
    }
    tmpmask = tmpmask / 2;
    mask += tmpmask;
    maxGroups *= 2;
  }
  cout << dec << "-1" << endl;
  return 0;
}
