#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n = 0;
  int k = 0;
  cin >> n >> k;
  vector<long long> ans(n);
  for (int i = 0; i < n; ++i) {
    int tmp = 0;
    for (int st = 0; st < 4; ++st) {
      cin >> tmp;
      ans[i] = (ans[i] << 8) + tmp;
      cin.ignore(1);
    }
  }
  sort(ans.begin(), ans.end());
  long long mask = 0;
  for (long long shift = 31; shift >= 0; --shift) {
    mask += 1LL << shift;
    int count = 1;
    for (int i = 1; i < n; ++i) {
      if ((ans[i] & mask) != (ans[i - 1] & mask)) ++count;
    }
    if (count == k) {
      vector<int> a;
      for (int y = 0; y < 4; ++y) {
        a.push_back(mask % 256);
        mask /= 256;
      }
      cout << a[3] << '.' << a[2] << '.' << a[1] << '.' << a[0] << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
