#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<unsigned int> g;
  for (int i = 0; i < n; i++) {
    int a, b, c, d;
    scanf("%d.%d.%d.%d", &a, &b, &c, &d);
    unsigned int ip = (a << 24) | (b << 16) | (c << 8) | d;
    g.push_back(ip);
  }
  unsigned int mask = 1 << 31;
  unsigned int submask = 0;
  for (int i = 1; i < 32; i++) {
    submask |= mask;
    set<unsigned int> networks;
    for (int j = 0; j < g.size(); j++) {
      networks.insert(submask & g[j]);
    }
    if (networks.size() == k) {
      int d = submask & 255;
      submask >>= 8;
      int c = submask & 255;
      submask >>= 8;
      int b = submask & 255;
      submask >>= 8;
      int a = submask & 255;
      cout << a << "." << b << "." << c << "." << d << endl;
      return 0;
    }
    mask >>= 1;
  }
  cout << -1 << endl;
  return 0;
}
