#include <bits/stdc++.h>
using namespace std;
void shikhar7s(long long int cas) {
  long long int n, k, i, j, c, a;
  cin >> n >> k;
  string s;
  vector<long long int> v[n];
  map<pair<pair<long long int, long long int>,
           pair<long long int, long long int> >,
      long long int>
      m;
  long long int p[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  for (i = 0; i < n; i++) {
    cin >> s;
    a = 0;
    for (j = 0; j < (long long int)s.length(); j++) {
      if (s[j] == '.') {
        a /= 10;
        v[i].push_back(a);
        a = 0;
      } else {
        a += (s[j] - '0');
        a *= 10;
      }
    }
    a /= 10;
    v[i].push_back(a);
  }
  long long int b, d, l, w, x, y, z, kk;
  a = 0;
  c = 0;
  b = 0;
  d = 0;
  for (j = 0; j < 4; j++) {
    for (i = 7; i >= 0; i--) {
      if (!j)
        a += p[i];
      else if (j == 1)
        b += p[i];
      else if (j == 2)
        c += p[i];
      else {
        if (!i) {
          cout << -1;
          return;
        }
        d += p[i];
      }
      kk = 0;
      m.clear();
      for (l = 0; l < n; l++) {
        w = (a & v[l][0]);
        x = (b & v[l][1]);
        y = (c & v[l][2]);
        z = (d & v[l][3]);
        if (!m.count(make_pair(make_pair(w, x), make_pair(y, z)))) {
          kk++;
          m[make_pair(make_pair(w, x), make_pair(y, z))] = 1;
        }
      }
      if (kk == k) {
        cout << a << "." << b << "." << c << "." << d;
        return;
      }
    }
  }
  cout << -1;
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int t = 1;
  long long int cas = 1;
  while (cas <= t) {
    shikhar7s(cas);
    cas++;
  }
  return 0;
}
