#include <bits/stdc++.h>
using namespace std;
long long ip[100100];
int main() {
  int n, k, m;
  char line[100];
  int nums[10];
  int ans[10];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> line;
    m = strlen(line);
    ip[i] = 0;
    for (int j = 0; j < m; j++)
      if (line[j] == '.') line[j] = ' ';
    sscanf(line, "%d%d%d%d", &nums[3], &nums[2], &nums[1], &nums[0]);
    for (int j = 24; j >= 0; j -= 8) ip[i] |= ((1LL * nums[j / 8]) << j);
  }
  long long mask = 0;
  bool ok = false;
  for (int i = 31; i >= 0; i--) {
    mask |= (1LL << i);
    set<long long> submask;
    for (int j = 0; j < n; j++) submask.insert(mask & ip[j]);
    if (submask.size() == k) {
      ok = true;
      break;
    }
  }
  for (int i = 0; i < 4; i++) {
    ans[3 - i] = (mask & 255);
    mask = (mask >> 8);
  }
  if (!ok)
    cout << "-1";
  else
    for (int i = 0; i < 4; i++)
      if (i)
        cout << "." << ans[i];
      else
        cout << ans[i];
  cout << endl;
}
