#include <bits/stdc++.h>
using namespace std;
map<pair<long long, long long>, long long> mp;
int a[100100][4];
int n, k;
int x[4];
void make(string &s) {
  int j = 0;
  x[0] = x[1] = x[2] = x[3] = 0;
  for (int i = 0; i < 32; i++) {
    x[j] = x[j] * 2 + (s[i] - '0');
    if (i % 8 == 7) j++;
  }
}
int main() {
  int y[4];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d.%d.%d.%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
  }
  string s(32, '0');
  long long hash1, hash2;
  for (int i = 0; i < 31; i++) {
    s[i] = '1';
    make(s);
    mp.clear();
    for (int i = 0; i < n; i++) {
      hash1 = hash2 = 0;
      for (int j = 0; j < 4; j++) {
        y[j] = x[j] & a[i][j];
        hash1 = hash1 * 991 + y[j];
        hash2 = hash2 * 457 + y[j];
      }
      mp[pair<long long, long long>(hash1, hash2)]++;
    }
    if (mp.size() == k) {
      printf("%d.%d.%d.%d\n", x[0], x[1], x[2], x[3]);
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
