#include <bits/stdc++.h>
using namespace std;
long long IP[100000], net[100000];
void p(long long n) {
  long long a, b, c, d;
  d = n % 256;
  n /= 256;
  c = n % 256;
  n /= 256;
  b = n % 256;
  n /= 256;
  a = n;
  cout << a << '.' << b << '.' << c << '.' << d << endl;
}
int main() {
  int n, k;
  set<long long> myset;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    int sz = str.size();
    for (int j = 0; j < sz; j++)
      if (str[j] == '.') str[j] = ' ';
    stringstream sstr;
    sstr << str;
    IP[i] = 0;
    for (int j = 0; j < 4; j++) {
      long long llk;
      sstr >> llk;
      IP[i] *= 256;
      IP[i] += llk;
    }
  }
  int s = 31;
  long long m = 1;
  for (int i = 0; i < s; i++) m <<= 1;
  while (s) {
    myset.clear();
    for (int i = 0; i < n; i++) {
      net[i] = IP[i] & m;
      myset.insert(net[i]);
    }
    if (myset.size() == k) {
      p(m);
      break;
    }
    s--;
    int ms = 1;
    for (int i = 0; i < s; i++) ms <<= 1;
    m += ms;
  }
  if (!s) cout << -1 << endl;
  return 0;
}
