#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  vector<unsigned int> ips;
  char _;
  for (int i = 0; i < n; i++) {
    unsigned int ip = 0;
    vector<int> A(4);
    cin >> A[3] >> _ >> A[2] >> _;
    cin >> A[1] >> _ >> A[0];
    for (int j = 0; j < 4; j++)
      for (int b = 0; b < 8; b++)
        if (A[j] & (1 << b)) ip |= 1 << (8 * j + b);
    ips.push_back(ip);
  }
  unsigned int mask = 0;
  for (int i = 31; i; i--) {
    mask |= 1 << i;
    set<unsigned int> nets;
    for (unsigned int ip : ips) nets.insert(mask & ip);
    if (int(nets.size()) == k) {
      vector<int> ans;
      for (int j = 0; j < 4; j++) {
        ans.push_back(mask % (1 << 8));
        mask /= 1 << 8;
      }
      cout << ans[3] << '.' << ans[2] << '.';
      cout << ans[1] << '.' << ans[0] << '\n';
      return;
    }
  }
  cout << "-1\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
};
