#include <bits/stdc++.h>
using namespace std;
long long s2num(string s) {
  long long num = 0;
  stringstream ss;
  ss.str(s);
  for (int i = 0; i < 4; i++) {
    int tmp;
    char c;
    ss >> tmp;
    if (i != 3) ss >> c;
    num = num * 256 + tmp;
  }
  return num;
}
string num2s(long long num) {
  stringstream ss;
  string s = "";
  for (int i = 0; i < 4; i++) {
    int tmp = num % 256;
    num /= 256;
    if (i != 0) {
      s = '.' + s;
    }
    ss << tmp;
    s = ss.str() + s;
    ss.str("");
  }
  return s;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<long long> addrs(0);
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    long long num = s2num(s);
    addrs.push_back(num);
  }
  long long msk = 0;
  int sz = 0;
  long long one = 1;
  for (int i = 31; i >= 0; i--) {
    msk = msk | (1LL << i);
    set<long long> nets;
    nets.clear();
    for (int j = 0; j < n; j++) {
      nets.insert(msk & addrs[j]);
    }
    sz = nets.size();
    if (sz >= k) break;
  }
  if (sz == k)
    cout << num2s(msk);
  else
    cout << -1;
}
