#include <bits/stdc++.h>
using namespace std;
vector<long long> addr;
void psh(string s) {
  long long res = 0, cur = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '.') {
      res *= 256;
      res += cur;
      cur = 0;
    } else {
      cur *= 10;
      cur += (s[i] - '0');
    }
  }
  res *= 256;
  res += cur;
  addr.push_back(res);
}
void outp(long long mask) {
  int d = mask % 256;
  mask /= 256;
  int c = mask % 256;
  mask /= 256;
  int b = mask % 256;
  mask /= 256;
  int a = mask;
  printf("%d.%d.%d.%d\n", a, b, c, d);
}
set<long long> s;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    psh(s);
  }
  long long mask = 0;
  for (int i = 31; i >= 0; i--) {
    mask += (1ll << i);
    s.clear();
    for (int i = 0; i < n; i++) {
      s.insert(addr[i] & mask);
    }
    if (s.size() == k) {
      outp(mask);
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
