#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
	int n[102], tn=0,ni=0,all = 0, i = 0,h=0;
	int data[1024][10];
	int tdata[10] = { 0 };
	
	//input
	while (1){
		cin >> n[ni];
		if (n[ni] == 0)break;
		tn += n[ni];

		for (; i < tn; i++){
			for (int j = 0; j < n[ni]; j++){
				cin >> data[i][j];
			}
		}
		ni++;
	}


	//output
	for (int k = 0; k < ni; k++){
		for (int i=0; i < n[k]; i++, all = 0,h++){
			for (int j = 0; j < n[k]; j++){
				printf("%5d", data[h][j]);
				all += data[h][j];
				tdata[j] += data[h][j];
			}
			printf("%5d\n", all);
		}

		all = 0;
		for (int i = 0; i < n[k]; i++){
			printf("%5d", tdata[i]);
			all += tdata[i];
		}
		printf("%5d\n", all);

		for (int i = 0; i < n[k]; i++){
			tdata[i] = 0;
		}
		all = 0;
	}
	return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;


int main(){
	
	int N,Q,M,Date[101],i,l,k;
	
	while(0<=scanf("%d%d",&N,&Q)){
		for(i=0;i<101;i++){
			Date[i]=0;
		}
		
		if((N==0) && (Q==0)){
			break;
		}
		
		for(k=0;k<N;k++){
			scanf("%d",&M);
			if(M==0){
				continue;
			}else{
				for(i=0;i<M;i++){
					scanf("%d",&l);
					++Date[l];
				}
			}
		}
		l=0;
		i=0;
		for(k=0;k<100;k++){
			if(i<Date[k] && Date[k]>=Q){
				i=Date[k];
				l=k;
			}
		}
		
		printf("%d\n",l);
	}
	
	return 0;
}