object Olymp{
  def main(args: Array[String]): Unit = {
    val Array(n, k) = readLine split (' ') map (_ toInt)
    val p = (readLine split (' ') map (_ toLong)) sorted
    var l = 0
    var balance: Long = 0
    var cnt = 1
    var num = p(0)
    for(r <- 1 until n){
      balance += (r-l) * (p(r)-p(r-1))
      while(balance > k){
        balance -= (p(r)-p(l))
        l += 1
      }
      if (r-l+1 > cnt){
        cnt = r-l+1
        num = p(r)
      }
    }
    println(cnt+" "+num)
  }
}

import java.util.Scanner

object C extends App {
  val sc = new Scanner(System.in)
  val n: Long = sc.nextLong
  val k: Long = sc.nextLong
  val initial: List[Long] = (for {i <- 1L to n} yield sc.nextLong).sortWith(_ < _).toList
  // TODO: Use foldl instead of foldr.
  val pairs = (initial :\ List[(Long, Long)]()) {
    case (next, Nil) => (next, 1L) :: Nil
    case (next, (c, n) :: xs) if next == c => (c, n + 1L) :: xs
    case (next, xs) => (next, 1L) :: xs
  }
  //println("pairs = ", pairs)
  val answers = ((0L, 0L, -0x40000000L, k, 0L, pairs) /: pairs) {
    case ((record, rec_i, prev, left0, sum0, initial0), (next, count)) =>
      //println("loop1: ", record, rec_i, prev, left0, sum0, initial0.head, next, count)
      val left1 = left0 - sum0 * (next - prev)
      val pay_whole = Stream.iterate((initial0, left1, sum0 + count)) {
	case ((c_i, n_i)::inits, left, sum) =>
	  //println("loop2: ", record, rec_i, prev, left0, sum0, initial0.head, next, count, c_i, n_i, left, sum)
	  val diff = next - c_i
	  (inits, left + diff * n_i, sum - n_i)
      }
      // (item array, operations left, sum)
      val (next_array, left2, sum2) =
	pay_whole.zip(pay_whole.tail).find {
	  case (_, (xs, left, sum)) => left >= 0L} match {
	  case Some((((c_i, n_i)::inits, left, sum), _)) =>
	    //println("loop3: ", record, rec_i, prev, left0, sum0, initial0.head, next, count, c_i, n_i, left, sum)
	    val diff = next - c_i
	    if (diff == 0 || left >= 0) {  // happens on the start of the loop.
	      ((c_i, n_i)::inits, left, sum)
	    } else {
	      val to_erase = (-left + diff - 1) / diff
	      if (to_erase < n_i) {
		((c_i, n_i - to_erase)::inits, left + diff * to_erase, sum - to_erase)
	      } else {
		(inits, left + diff * n_i, sum - n_i)
	      }
	    }
	}
      assert (left2 >= 0L, {println("assert broken")})
      val (new_record:Long, new_rec_i:Long) =
	if (record < sum2) (sum2, next) else (record, rec_i)
      (new_record, new_rec_i, next, left2, sum2, next_array)
  }
  printf("%d %d\n", answers._1, answers._2)
}

object Olymp{
  def main(args: Array[String]): Unit = {
    val Array(n, k) = readLine split (' ') map (_ toInt)
    val p = (readLine split (' ') map (_ toInt)) sorted
    var l = 0
    var balance = 0
    var cnt = 1
    var num = p(0)
    for(r <- 1 until n){
      balance += (r-l) * (p(r)-p(r-1))
      while(balance > k){
        balance -= (p(r)-p(l))
        l += 1
      }
      if (r-l+1 > cnt){
        cnt = r-l+1
        num = p(r)
      }
    }
    println(cnt+" "+num)
  }
}


import collection.mutable.Map
import collection.mutable.ArrayBuffer

object B
{
    def main(args: Array[String])
    {
        val MAXI = 200000
        val prePrimeBuffer = ArrayBuffer[Int]()
        val visit=new Array[Boolean](MAXI+1)
        prePrimeBuffer += 2
        for(i<-3 until MAXI by 2 if !visit(i))
        {
            prePrimeBuffer+=i
            var j=2
            while(i*j<=MAXI) {visit(i*j)=true;j+=1}
        }

        val prePrime = prePrimeBuffer toArray
        val Array(n,m) = readLine.split(" ").map(_.toInt)
        val mat = Array.ofDim[Int](n,m)

        for(i <- 0 until n)
        {
            mat(i) = readLine.split(" ").map(_.toInt)
        }
        val matT = mat.transpose
        //print(mat map (_ map ("%2d" format _) mkString " ") mkString "\n")

        def nextPrime(target:Int):Int={
            var i=0
            var j=prePrime.size-1
            while(i<j){
                val mid=(i+j)/2
                if(prePrime(mid)==target) return prePrime(mid)
                else if(prePrime(mid)<target) i=mid+1
                else j=mid
            }

            prePrime(i)
        }

        def slove(mm: Array[Array[Int]]): List[Int] =
        {
            val sz = mm.size
            val ans = for(i <- 0 until sz)
                    yield (mm(i).map((x) => (nextPrime(x) - x))).sum
            ans.toList
        }
        val aa = slove(mat)
        val bb = slove(matT)
        println(Array(aa.min,bb.min).min)
    }
}

import scala.io.Source

object Solution extends App {
  val in = Source.stdin.getLines()
  val Array(n, m) = in.next().split(' ').map(_.toInt)
  val lines = (1 to n).map(_ => in.next().split(' ').map(_.toInt))
  val max = lines.map(_.max).max

  def isPrime(a: Int) = a == 2 || (a != 1 && a % 2 != 0 && (3 until a by 2).forall(a % _ != 0))

  val nextPrime = Stream.from(max + 1 - max % 2, 2).find(isPrime).get
  val sieve = Array.ofDim[Boolean](nextPrime + 1)
  sieve(0) = true
  sieve(1) = true
  (2 until nextPrime).foreach{ j =>
    if (!sieve(j))
      (j * 2 until nextPrime by j).foreach(i => sieve(i) = true)
  }
  val set = scala.collection.immutable.TreeSet(sieve.indices.filterNot(sieve):_*)
  val smallLine = lines.foldLeft(Int.MaxValue){
    case(acc, line) => Math.min(acc, line.map(i => set.iteratorFrom(i).next() - i).sum)
  }
  val smallColumn = (0 until m).foldLeft(Int.MaxValue) {
    case(acc, i) => Math.min(acc, lines.map(_(i)).map(i => set.iteratorFrom(i).next() - i).sum)
  }

  println(Math.min(smallColumn, smallLine))
}

import collection.mutable.Map
import collection.mutable.ArrayBuffer

object test6 extends App {
    val max=200000
    val ab=ArrayBuffer[Int]()
    val v=new Array[Boolean](max+1)
    ab+=2
    for(i<-3 until max by 2 if !v(i)){
        ab+=i
        var j=2
        while(i*j<=max) {v(i*j)=true;j+=1}
    }

    def next(target:Int):Int={
        var i=0
        var j=ab.size-1
        while(i<j){
            val mid=(i+j)/2
            if(ab(mid)==target) return ab(mid)
            else if(ab(mid)<target) i=mid+1
            else j=mid
        }

        ab(i)
    }

    val Array(n,m)=readLine.split(" ").map(_.toInt)
    val mat=new Array[Array[Int]](n)

    for(i<-0 until n) mat(i)=readLine.split(" ").map(_.toInt).map(i=>next(i)-i)
    var min=(for(i<-0 until n) yield mat(i).sum).min
    min=math.min(min,(for(j<-0 until m) yield  (for(i<-0 until n) yield mat(i)(j)).sum).min)
    println(min)
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.Random
import scala.util.control.Breaks._
import scala.collection.mutable.ArrayBuffer
import java.util.Arrays.binarySearch

object P271B extends App {

  object PrimeNumber {
    def apply(n: Int = 1000000): Array[Int] = sieve(n)

    def sieve(n: Int): Array[Int] = {
      val primes = new ArrayBuffer[Int]
      val isPrime = Array.fill(n + 1)(true)

      @tailrec
      def loop(i: Int): Unit = {

        @tailrec
        def innerLoop(j: Int): Unit = {
          if (j > n) ()
          else {
            isPrime(j) = false
            innerLoop(j + i)
          }
        }

        if (i > n) ()
        else {
          if (isPrime(i)) {
            primes += i
            innerLoop(i * 2)
          }
          loop(i + 1)
        }
      }

      loop(2)
      primes.toArray
    }
  }

  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val Ps: Array[Int] = PrimeNumber(100003)

  val nextPrime: Int => Int = { x =>
    val i = binarySearch(Ps, x)
    if (i >= 0) x
    else Ps(-1 - i)
  }
  val numMove: Int => Int = { x => nextPrime(x) - x }

  val N, M = sc.nextInt
  val Matrix = List.fill(N, M)(numMove(sc.nextInt))
  val rowsAndCols = Matrix ++ Matrix.transpose
  val res = rowsAndCols.map(_.sum).min

  out.println(res)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.Random
import scala.util.control.Breaks._
import scala.collection.mutable.ArrayBuffer
import java.util.Arrays.binarySearch

object P271B extends App {

  object PrimeNumber {
    def apply(n: Int = 1000000): Array[Int] = sieve(n)

    def sieve(n: Int): Array[Int] = {
      val primes = new ArrayBuffer[Int]
      val isPrime = Array.fill(n + 1)(true)

      @tailrec
      def loop(i: Int): Unit = {

        @tailrec
        def innerLoop(j: Int): Unit = {
          if (j > n) ()
          else {
            isPrime(j) = false
            innerLoop(j + i)
          }
        }

        if (i > n) ()
        else {
          if (isPrime(i)) {
            primes += i
            innerLoop(i * 2)
          }
          loop(i + 1)
        }
      }

      loop(2)
      primes.toArray
    }
  }

  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val Ps: Array[Int] = PrimeNumber(100003)

  val nextPrime: Int => Int = { x =>
    val i = binarySearch(Ps, x)
    if (i >= 0) x
    else Ps(-1 - i)
  }
  val numMove: Int => Int = { x => nextPrime(x) - x }

  val N, M = sc.nextInt
  val Matrix = Vector.fill(N, M)(numMove(sc.nextInt))
  val rowsAndCols = Matrix ++ Matrix.transpose
  val res = rowsAndCols.map(_.sum).min

  out.println(res)
  out.close
}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P271A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val Start = sc.nextInt

  def solve: Int = {
    @tailrec
    def loop(y: Int): Int =
      if (y.toString.groupBy(identity[Char]).size == 4) y
      else loop(y + 1)
    loop(Start + 1)
  }

  out.println(solve)
  out.close
}

import collection.mutable.Map
import collection.mutable.ArrayBuffer

object B
{
    def main(args: Array[String])
    {
        val SIZE = 102400
        val sieve = Array.fill[Boolean](SIZE+5)(true)

        def prime_gen(x: Int): Unit =
        {
            if(sieve(x)) ((2 to SIZE/x) par) map {_ * x} map {sieve( _ ) = false}
            if(x+1 < SIZE) prime_gen(x+1)
        }

        def lower_bound(a: List[Int], l:Int, r:Int, v:Int): Int =
        {
            val mid = (l+r)>>1
            if(l>r) l
            else if(a(mid) < v) lower_bound(a, mid+1, r, v)
            else lower_bound(a, l, mid-1, v)
        }

        prime_gen(2)
        val prime_list = ((2 to SIZE) toList) filter {sieve(_)}
        val prime_list_len = prime_list.length

        val Array(n,m) = readLine.split(" ").map(_.toInt)
        val mat = Array.ofDim[Int](n,m)

        for(i <- 0 until n)
        {
            mat(i) = readLine.split(" ").map(_.toInt)
        }
        val matT = mat.transpose
        //print(mat map (_ map ("%2d" format _) mkString " ") mkString "\n")

        def nextPrime(v: Int): Int =
        {
            prime_list(lower_bound(prime_list,0,prime_list_len-1,v))
        }

        def slove(mm: Array[Array[Int]]): List[Int] =
        {
            val sz = mm.size
            val ans = ((1 until sz) par) map
                      {
                            mm(_) map
                            {
                                ((x) => (nextPrime(x) - x))
                            } sum
                      }
            ans.toList
        }

        val aa = slove(mat)
        val bb = slove(matT)
        println(Array(aa.min,bb.min).min)
    }
}

import collection.mutable.Map
import collection.mutable.ArrayBuffer

object B
{
    def main(args: Array[String])
    {
        val SIZE = 102400
		val sieve = Array.fill[Boolean](SIZE+5)(true)

		def prime_gen(x: Int): Unit =
		{
			if(sieve(x)) ((2 to SIZE/x) toList) map {_ * x} map {sieve( _ ) = false}
			if(x+1 < SIZE) prime_gen(x+1)
		}

		def upper_bound(a: List[Int], l:Int, r:Int, v:Int): Int =
		{
			val mid = (l+r)>>1
			if(l>r) l
			else if(a(mid) <= v) upper_bound(a, mid+1, r, v)
			else upper_bound(a, l, mid-1, v)
		}

		prime_gen(2)
		val prime_list = ((2 to SIZE) toList) filter {sieve(_)}
		val prime_list_len = prime_list.length

        val Array(n,m) = readLine.split(" ").map(_.toInt)
        val mat = Array.ofDim[Int](n,m)

        for(i <- 0 until n)
        {
            mat(i) = readLine.split(" ").map(_.toInt)
        }
        val matT = mat.transpose
        //print(mat map (_ map ("%2d" format _) mkString " ") mkString "\n")

		def nextPrime(v: Int): Int =
        {
			prime_list(upper_bound(prime_list,0,prime_list_len-1,v))
		}

        def slove(mm: Array[Array[Int]]): List[Int] =
        {
            val sz = mm.size
			val ans = for(i <- 0 until sz)
                    yield (mm(i).map((x) => (nextPrime(x) - x))).sum
            ans.toList
        }

        //println(nextPrime(3))

        val aa = slove(mat)
        val bb = slove(matT)
        println(Array(aa.min,bb.min).min)
    }
}

import collection.mutable.Map
import collection.mutable.ArrayBuffer

object test6 extends App {
    val max=100000
    val ab=ArrayBuffer[Int]()
    val v=new Array[Boolean](max+1)
    ab+=2
    for(i<-3 until max by 2 if !v(i)){
        ab+=i
        var j=2
        while(i*j<=max) {v(i*j)=true;j+=1}
    }

    def next(target:Int):Int={
        var i=0
        var j=ab.size-1
        while(i<j){
            val mid=(i+j)/2
            if(ab(mid)==target) return ab(mid)
            else if(ab(mid)<target) i=mid+1
            else j=mid
        }

        ab(i)
    }

    val Array(n,m)=readLine.split(" ").map(_.toInt)
    val mat=new Array[Array[Int]](n)

    for(i<-0 until n) mat(i)=readLine.split(" ").map(_.toInt).map(i=>next(i)-i)
    var min=(for(i<-0 until n) yield mat(i).sum).min
    min=math.min(min,(for(j<-0 until m) yield  (for(i<-0 until n) yield mat(i)(j)).sum).min)
    println(min)
}