#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<long long int> masks;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    long long int num = 0;
    long long int mask_int = 0;
    for (int j = 0; j < s.length(); j++) {
      if (s[j] == '.') {
        mask_int = mask_int * 256 + num;
        num = 0;
        continue;
      }
      num = num * 10 + s[j] - '0';
    }
    mask_int = mask_int * 256 + num;
    masks.push_back(mask_int);
  }
  long long int bit = 1ll << 31;
  long long int num = 0;
  long long int ans = -1;
  while (bit > 1) {
    num += bit;
    unordered_map<long long int, int> network;
    int cnt = 0;
    for (long long int i : masks) {
      long long int net = i & num;
      if (!network[net]) cnt++;
      network[net]++;
    }
    if (cnt == k) {
      ans = num;
      break;
    }
    bit /= 2;
  }
  if (ans == -1) {
    cout << ans << endl;
    return 0;
  }
  vector<long long int> nets;
  for (int i = 0; i < 4; i++) {
    nets.push_back(ans % 256);
    ans /= 256;
  }
  for (int i = 3; i >= 0; i--) {
    cout << nets[i];
    if (i) cout << ".";
  }
  cout << endl;
  return 0;
}
