#include <bits/stdc++.h>
const int mod = 1e9 + 7;
long long int power(long long int a, long long int b) {
  long long int r = 1;
  for (; b; b >>= 1) {
    if (b & 1) r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
using namespace std;
array<int, 4> bitcon(string ans) {
  array<int, 4> send = {0, 0, 0, 0};
  int i = 0, p = 0;
  while (i < int(ans.size())) {
    string s1 = "";
    int a;
    while (i < ans.size() && ans[i] != '.') s1.push_back(ans[i++]);
    i++;
    a = stoi(s1);
    send[p++] = a;
  }
  return send;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  bool isit = true;
  cin >> n >> k;
  vector<array<int, 4>> v(n);
  string ans = "";
  for (int i = 0; i < n; i++) {
    cin >> ans;
    v[i] = bitcon(ans);
  }
  array<int, 4> an = {0, 0, 0, 0};
  int p;
  for (int i = 31; i >= 1; i--) {
    for (int i = 0; i < 4; i++) {
      if (an[i] == 0) p = 128;
      if (an[i] != 255) {
        an[i] += p, p /= 2;
        break;
      }
    }
    set<array<int, 4>> s;
    for (auto it : v) {
      array<int, 4> r;
      for (int j = 0; j < 4; j++) r[j] = an[j] & it[j];
      s.insert(r);
    }
    if (s.size() == k) {
      for (int i = 0; i < 4; i++) cout << an[i] << ". "[i == 3];
      return 0;
    }
  outer:
    continue;
  }
  cout << -1 << endl;
  return 0;
}
