#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  char c;
  unsigned int mask;
  int u;
  vector<unsigned int> ip;
  set<unsigned int> net;
  cin >> n >> k;
  scanf("%c", &c);
  unsigned int x, x1;
  for (int i = 0; i < n; i++) {
    c = 0;
    x = 0;
    x1 = 0;
    while (c != '\n') {
      scanf("%c", &c);
      if ((c != '.') && (c != '\n')) {
        x *= 10;
        x += (c - '0');
      } else {
        x1 = x1 << 8;
        x1 += x;
        x = 0;
      }
    }
    ip.push_back(x1);
  }
  for (int len = 1; len < 32; len++) {
    mask = 1;
    for (int y = 1; y < len; y++) {
      mask = mask << 1;
      mask++;
    }
    mask = mask << (32 - len);
    unsigned int v = 0;
    for (int i = 0; i < n; i++) {
      v = ip[i];
      v &= mask;
      net.insert(v);
    }
    u = net.size();
    if (u == k) {
      unsigned int xx[4];
      xx[3] = mask % 256;
      mask /= 256;
      xx[2] = mask % 256;
      mask /= 256;
      xx[1] = mask % 256;
      mask /= 256;
      xx[0] = mask;
      cout << xx[0] << "." << xx[1] << "." << xx[2] << "." << xx[3] << endl;
      return 0;
    }
    if (u > k) {
      cout << "-1" << endl;
      return 0;
    }
    net.clear();
  }
  cout << "-1" << endl;
  return 0;
}
