K,_=gets.chomp.split(' ').map{|n| n.to_i}

if(K<=500)
	puts K
	for i in 1..K
		puts ([i]*K).join(" ")
	end
else
	puts 500
	arr = Array.new(500){|i| i.to_i + 1}
	rem = K - 500
	for i in 1..500
		if(i%2 == 1)
			puts arr.rotate(i-1).join(" ")
		else
			tmp = arr.rotate(i-1)
			for r in 1..rem
				tmp[r-1-(i-1)] = r+500
			end
			puts tmp.rotate(i-1).join(" ")
		end
	end
end

