#include <bits/stdc++.h>
const int MAXN = 1e5 + 10;
using namespace std;
int N, K;
int A[MAXN][4];
struct xxx {
  int a[4];
  bool operator<(const xxx &T) const {
    for (int c = 0; c < 3; c++)
      if (a[c] != T.a[c]) return a[c] < T.a[c];
    return a[3] < T.a[3];
  }
};
map<xxx, int> M;
int main() {
  scanf("%d %d", &N, &K);
  for (int c = 0; c < N; c++)
    for (int d = 0; d < 4; d++) scanf("%*c%d", &A[c][d]);
  for (int c = 0; c < 4; c++) {
    int run = 0;
    for (int d = 7; d >= 0; d--) {
      M.clear();
      run |= 1 << d;
      for (int e = 0; e < N; e++) {
        xxx tmp;
        for (int f = 0; f < c; f++) tmp.a[f] = A[e][f];
        tmp.a[c] = A[e][c] & run;
        for (int f = c + 1; f < 4; f++) tmp.a[f] = 0;
        M[tmp] = 1;
      }
      if (M.size() > K or (c == 3 and d == 0)) {
        printf("-1\n");
        return 0;
      }
      if (M.size() == K) {
        int sol[4] = {};
        for (int e = 0; e < c; e++) sol[e] = 255;
        sol[c] = run;
        printf("%d.%d.%d.%d\n", sol[0], sol[1], sol[2], sol[3]);
        return 0;
      }
    }
  }
}
