#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  string s;
  vector<long long> a(n);
  long long buf, b;
  for (int i = 0; i < n; i++) {
    cin >> s;
    s += ".";
    buf = 0;
    b = 0;
    for (int j = 0; j < s.length(); j++) {
      if (s[j] == '.') {
        buf = buf * 256 + b;
        b = 0;
      } else
        b = b * 10 + (s[j] - '0');
    }
    a[i] = buf;
  }
  long long mask = 0;
  for (long long q = 31; q > 0; q--) {
    mask += (1LL << q);
    set<long long> arr;
    for (int i = 0; i < n; i++) arr.insert(a[i] & mask);
    if (arr.size() == k) {
      vector<long long> aa(4);
      for (int i = 0; i < 4; i++) {
        aa[i] = mask % 256;
        mask /= 256;
      }
      for (int i = 4 - 1; i >= 0; i--) {
        cout << aa[i];
        if (i != 0) cout << '.';
      }
      return 0;
    }
  }
  cout << -1;
}
