#include <bits/stdc++.h>
using namespace std;
vector<string> split(string s, string x = ".") {
  vector<string> res;
  for (int i(0), _n(((int)(s.size()))); i < _n; ++i) {
    string a;
    while (i < (int)s.size() && x.find(s[i]) == string::npos) a += s[i++];
    if (!a.empty()) res.push_back((a));
  }
  return res;
}
long long toint(string s) {
  long long r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
string tostr(long long a) {
  if (a == 0) return "0";
  string ret;
  for (long long i = a; i > 0; i = i / 10) ret.push_back((i % 10) + 48);
  reverse(ret.begin(), ret.end());
  return ret;
}
vector<long long> v;
long long n, k, t1, t2, t3, t4;
string s;
void fun(long long n) {
  vector<string> vvv;
  for (int i(0), _n(4); i < _n; ++i) {
    vvv.push_back(tostr(n % 256));
    n /= 256;
  }
  reverse(vvv.begin(), vvv.end());
  cout << vvv[0];
  for (int i(0), _n(3); i < _n; ++i) cout << "." << vvv[i + 1];
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i(0), _n(n); i < _n; ++i) {
    cin >> s;
    vector<string> vv = split(s);
    long long last = 0;
    for (int i(0), _n(((int)(vv.size()))); i < _n; ++i) {
      last = toint(vv[i]) + last * (1LL << 8);
    }
    v.push_back(last);
  }
  long long cur = 0;
  for (int i = 31; i >= 0; i--) {
    cur += 1LL << i;
    map<long long, int> M;
    for (int j(0), _n(n); j < _n; ++j) {
      M[v[j] & cur] = 1;
    }
    if (((int)(M.size())) == k) {
      fun(cur);
      return 0;
    }
  }
  cout << -1;
}
