#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, k;
unsigned int a[MAXN], subnetmask, Ans;
string s[MAXN];
set<unsigned int> S;
bool found = 0;
int toint(string t) {
  unsigned int n = 0, temp = 1;
  for (int i = (int)t.size() - 1; i >= 0; i--) {
    n += (temp * (t[i] - '0'));
    temp *= 10;
  }
  return n;
}
int change(string t) {
  string temp;
  unsigned int i = 0, n = 0;
  while (t[i] != '.') i++;
  temp = t.substr(0, i);
  n += toint(temp) * (1 << 24);
  i++;
  t = t.substr(i);
  i = 0;
  while (t[i] != '.') i++;
  temp = t.substr(0, i);
  n += toint(temp) * (1 << 16);
  i++;
  t = t.substr(i);
  i = 0;
  while (t[i] != '.') i++;
  temp = t.substr(0, i);
  n += toint(temp) * (1 << 8);
  i++;
  t = t.substr(i);
  n += toint(t);
  return n;
}
void IP() {
  int ans1 = Ans % (1 << 8);
  Ans /= (1 << 8);
  int ans2 = Ans % (1 << 8);
  Ans /= (1 << 8);
  int ans3 = Ans % (1 << 8);
  Ans /= (1 << 8);
  cout << Ans << '.' << ans3 << '.' << ans2 << '.' << ans1 << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    a[i] = change(s[i]);
  }
  for (int i = 1; i <= 31; i++) subnetmask += (1 << i);
  for (int i = 1; i <= 31; i++) {
    S.clear();
    for (int j = 0; j < n; j++) S.insert(subnetmask & a[j]);
    if ((int)S.size() == k) {
      Ans = subnetmask;
      found = 1;
    }
    subnetmask -= (1 << i);
  }
  if (found)
    IP();
  else
    cout << -1 << endl;
  return 0;
}
