#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> ip(n, vector<int>(4));
  for (auto &a : ip) {
    cin >> a[0];
    char c;
    for (int i = 1; i < 4; i++) {
      cin >> c >> a[i];
    }
  }
  vector<int> mask(4);
  for (int b = 0; b < 32; b++) {
    for (int i = 0; i < 4; i++) {
      if (mask[i] == 0) {
        mask[i] = 1 << 7;
        break;
      }
      if ((mask[i] & 255) != 255) {
        int d = mask[i] & -mask[i];
        mask[i] |= d >> 1;
        break;
      }
    }
    set<vector<int>> se;
    for (auto &a : ip) {
      vector<int> foo(4);
      for (int i = 0; i < 4; i++) {
        foo[i] = mask[i] & a[i];
      }
      se.emplace(foo);
    }
    if ((int)se.size() == k && mask[3] != 255) {
      cout << mask[0];
      for (int i = 1; i < 4; i++) {
        cout << '.' << mask[i];
      }
      return 0;
    }
  }
  cout << -1;
  return 0;
}
