#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long> v;
  long long t, p;
  char c;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    p = 0ll;
    cin >> t >> c;
    p = ((p << 8) | t);
    cin >> t >> c;
    p = ((p << 8) | t);
    cin >> t >> c;
    p = ((p << 8) | t);
    cin >> t;
    p = ((p << 8) | t);
    v.push_back(p);
  }
  long long sub = 0ll;
  for (long long i = 31; i >= 0; i--) {
    sub = (sub | (1ll << i));
    set<long long> s;
    for (int j = 0; j < n; j++) {
      s.insert(v[j] & sub);
    }
    if (s.size() == k)
      break;
    else if (s.size() > k) {
      sub = -1;
      break;
    }
  }
  if (sub == -1)
    cout << "-1" << endl;
  else {
    vector<long long> last;
    last.push_back(sub & ((1ll << 8) - 1ll));
    sub = (sub >> 8);
    last.push_back(sub & ((1ll << 8) - 1ll));
    sub = (sub >> 8);
    last.push_back(sub & ((1ll << 8) - 1ll));
    sub = (sub >> 8);
    last.push_back(sub & ((1ll << 8) - 1ll));
    cout << last[3] << "." << last[2] << "." << last[1] << "." << last[0]
         << endl;
  }
  return 0;
}
