Problem:
Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1

Solution in Scala (2.11.7) (judge verdict: Runtime Error)

import scala.io.StdIn

object Main extends App {
  val Array(n, p) = StdIn.readLine.split(" ").map(_.toLong)

  val ans = if(n == 1) p
            else (1 to 1000).filter(i =>
              p % math.pow(i,n-1).toLong == 0 &&
              (p / math.pow(i,n-1) % i == 0)).max

  println(ans)
}