#include <bits/stdc++.h>
using namespace std;
unsigned int set_bit(unsigned int c, short bit_n, bool bit) {
  bit_n = 32 - 1 - bit_n;
  if (bit == 0) {
    return c & (~(1 << bit_n));
  } else {
    return c | (1 << bit_n);
  }
}
bool get_bit(unsigned int x, short i) { return x & (1 << (31 - i)); }
string print_bits(unsigned int x) {
  stringstream ss;
  for (int i = 0; i < 32; ++i) {
    ss << get_bit(x, i);
  }
  return ss.str();
}
unsigned int rand_ip(void) {
  unsigned int ip = rand() % 256;
  ip = rand() % 256 + ip * 256;
  ip = rand() % 256 + ip * 256;
  ip = rand() % 256 + ip * 256;
  return ip;
}
unsigned int ip_from_str(char* s) {
  unsigned int x1, x2, x3, x4;
  sscanf(s, "%u.%u.%u.%u", &x1, &x2, &x3, &x4);
  unsigned int ip = x1;
  ip = x2 + ip * 256;
  ip = x3 + ip * 256;
  ip = x4 + ip * 256;
  return ip;
}
string ip_to_str(unsigned int ip) {
  unsigned int x4 = ip & 0xff;
  ip >>= 8;
  unsigned int x3 = ip & 0xff;
  ip >>= 8;
  unsigned int x2 = ip & 0xff;
  ip >>= 8;
  unsigned int x1 = ip & 0xff;
  ip >>= 8;
  stringstream ss;
  ss << x1 << "." << x2 << "." << x3 << "." << x4;
  return ss.str();
}
int main(void) {
  vector<unsigned int> arr_ip;
  vector<vector<unsigned int>*> kind_arr_ip;
  vector<vector<unsigned int>*> new_kind_arr_ip;
  unsigned int n_ip, k_net;
  scanf("%u %u\n", &n_ip, &k_net);
  for (int i = 0; i < n_ip; ++i) {
    string s;
    getline(cin, s);
    unsigned int ip = ip_from_str((char*)s.c_str());
    arr_ip.push_back(ip);
  }
  for (int i = 0; i < n_ip; ++i) {
  }
  kind_arr_ip.push_back(&arr_ip);
  unsigned int alone_n = 0;
  unsigned int full_mask = 0;
  int mask_len;
  for (mask_len = 1; mask_len <= 31; ++mask_len) {
    unsigned int mask = set_bit(0, mask_len - 1, 1);
    full_mask |= mask;
    for (int i_arr = 0; i_arr < kind_arr_ip.size(); ++i_arr) {
      for (int wanted_bit = 0; wanted_bit <= 1; ++wanted_bit) {
        vector<unsigned int>* split_arr_ip = new vector<unsigned int>();
        for (int i_ip = 0; i_ip < (*(kind_arr_ip[i_arr])).size(); ++i_ip) {
          unsigned int x = (*(kind_arr_ip[i_arr]))[i_ip];
          if (bool(x & mask) == bool(wanted_bit)) {
            split_arr_ip->push_back(x);
          }
        }
        if (split_arr_ip->size() == 0) {
          delete split_arr_ip;
        } else if (split_arr_ip->size() == 1) {
          alone_n += 1;
          delete split_arr_ip;
        } else {
          new_kind_arr_ip.push_back(split_arr_ip);
        }
      }
    }
    kind_arr_ip.swap(new_kind_arr_ip);
    for (int i_arr = 0; i_arr < new_kind_arr_ip.size(); ++i_arr) {
      new_kind_arr_ip[i_arr]->clear();
    }
    new_kind_arr_ip.clear();
    int n_kind = kind_arr_ip.size() + alone_n;
    if (n_kind == k_net) {
      cout << ip_to_str(full_mask) << endl;
      break;
    }
  }
  if (mask_len == 32) {
    cout << "-1" << endl;
  }
  return 0;
}
