#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K, A, B, C, D;
  cin >> N >> K;
  vector<tuple<int, int, int, int> > V(N);
  for (int i = 0; i < N; i++) {
    scanf("%d.%d.%d.%d", &A, &B, &C, &D);
    V[i] = tuple<int, int, int, int>(A, B, C, D);
  }
  A = B = C = D = 0;
  for (int i = 7; i >= 0; i--) {
    A |= 1 << i;
    map<tuple<int, int, int, int>, int> mp;
    for (int j = 0; j < N; j++)
      mp[tuple<int, int, int, int>(A & get<0>(V[j]), B & get<1>(V[j]),
                                   C & get<2>(V[j]), D & get<3>(V[j]))]++;
    if (mp.size() == K) {
      printf("%d.%d.%d.%d\n", A, B, C, D);
      return 0;
    }
  }
  for (int i = 7; i >= 0; i--) {
    B |= 1 << i;
    map<tuple<int, int, int, int>, int> mp;
    for (int j = 0; j < N; j++)
      mp[tuple<int, int, int, int>(A & get<0>(V[j]), B & get<1>(V[j]),
                                   C & get<2>(V[j]), D & get<3>(V[j]))]++;
    if (mp.size() == K) {
      printf("%d.%d.%d.%d\n", A, B, C, D);
      return 0;
    }
  }
  for (int i = 7; i >= 0; i--) {
    C |= 1 << i;
    map<tuple<int, int, int, int>, int> mp;
    for (int j = 0; j < N; j++)
      mp[tuple<int, int, int, int>(A & get<0>(V[j]), B & get<1>(V[j]),
                                   C & get<2>(V[j]), D & get<3>(V[j]))]++;
    if (mp.size() == K) {
      printf("%d.%d.%d.%d\n", A, B, C, D);
      return 0;
    }
  }
  for (int i = 7; i >= 1; i--) {
    D |= 1 << i;
    map<tuple<int, int, int, int>, int> mp;
    for (int j = 0; j < N; j++)
      mp[tuple<int, int, int, int>(A & get<0>(V[j]), B & get<1>(V[j]),
                                   C & get<2>(V[j]), D & get<3>(V[j]))]++;
    if (mp.size() == K) {
      printf("%d.%d.%d.%d\n", A, B, C, D);
      return 0;
    }
  }
  cout << -1 << endl;
}
