#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
T read(T a = T()) {
  return ((cin >> a), a);
}
void read(vector<int>& a) {
  for (int& i : a) cin >> i;
}
void read(int& arg) { cin >> arg; }
template <typename... T>
void read(T&... args) {
  ((read(args)), ...);
}
void solve() {
  char c;
  int n, k, x, y, z, w;
  read(n, k);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    read(x, c, y, c, z, c, w);
    a[i] = x << 24 | y << 16 | z << 8 | w;
  }
  sort(a.begin(), a.end());
  int mask = 0;
  for (int i = 31; i > 0; i--) {
    mask |= 1 << i;
    int cnt = 1;
    for (int j = 0; j < n - 1; j++) {
      cnt += (a[j] & mask) != (a[j + 1] & mask);
    }
    if (cnt == k) {
      printf("%d.%d.%d.%d", mask >> 24 & 255, mask >> 16 & 255, mask >> 8 & 255,
             mask & 255);
      return;
    }
  }
  cout << -1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  while (t--) solve();
  return 0;
}
