#include <bits/stdc++.h>
using namespace std;
int a[31][4] = {
    {128, 0, 0, 0},       {192, 0, 0, 0},       {224, 0, 0, 0},
    {240, 0, 0, 0},       {248, 0, 0, 0},       {252, 0, 0, 0},
    {254, 0, 0, 0},       {255, 0, 0, 0},       {255, 128, 0, 0},
    {255, 192, 0, 0},     {255, 224, 0, 0},     {255, 240, 0, 0},
    {255, 248, 0, 0},     {255, 252, 0, 0},     {255, 254, 0, 0},
    {255, 255, 0, 0},     {255, 255, 128, 0},   {255, 255, 192, 0},
    {255, 255, 224, 0},   {255, 255, 240, 0},   {255, 255, 248, 0},
    {255, 255, 252, 0},   {255, 255, 254, 0},   {255, 255, 255, 0},
    {255, 255, 255, 128}, {255, 255, 255, 192}, {255, 255, 255, 224},
    {255, 255, 255, 240}, {255, 255, 255, 248}, {255, 255, 255, 252},
    {255, 255, 255, 254}};
int main() {
  int n, k;
  cin >> n >> k;
  int ar[n][4];
  char c;
  for (long long i = 0; i < n; i++)
    cin >> ar[i][0] >> c >> ar[i][1] >> c >> ar[i][2] >> c >> ar[i][3];
  for (long long i = 0; i < 31; i++) {
    set<long> s;
    for (long long j = 0; j < n; j++) {
      long x;
      int q1, q2, q3, q4;
      q1 = (ar[j][0] & a[i][0]);
      q2 = (ar[j][1] & a[i][1]);
      q3 = (ar[j][2] & a[i][2]);
      q4 = (ar[j][3] & a[i][3]);
      x = q4 | (q3 << 8) | (q2 << 16) | (q1 << 24);
      s.insert(x);
    }
    if (s.size() == k) {
      cout << a[i][0] << '.' << a[i][1] << '.' << a[i][2] << '.' << a[i][3]
           << endl;
      return 0;
    }
  }
  cout << "-1" << endl;
}
