#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int n, k;
string s[100005];
long long ip[100005];
void solve() {
  for (int i = 0; i < n; i++) {
    long long val = 0, p = 1LL << 24;
    int sz = s[i].size();
    for (int j = 0; j < sz; j++) {
      long long t = 0;
      while (j < sz && s[i][j] != '.') {
        t = 10 * t + (s[i][j] - '0');
        j++;
      }
      val += t * p;
      p >>= 8;
    }
    ip[i] = val;
  }
  for (long long p = (1LL << 31), msk = p; p > 0; p >>= 1LL, msk += p) {
    set<long long> S;
    for (int i = 0; i < n; i++) {
      S.insert(msk & ip[i]);
    }
    if ((int)S.size() == k) {
      long long m = 1LL << 24;
      for (int i = 0; i < 4; i++) {
        cout << msk / m;
        msk %= m;
        m >>= 8LL;
        if (i < 3) {
          cout << ".";
        }
      }
      return;
    }
  }
  cout << -1 << endl;
}
int main() {
  cin.sync_with_stdio(false);
  while (cin >> n >> k) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    solve();
  }
}
