
import Foundation
func readInt() -> Int {
    return Int(readLine()!)!
}
 
func readInts() -> [Int] {
    return readLine()!.split(separator: " ").map { Int($0)! }
}
 
func readTwoInts() -> (a: Int, b: Int) {
    let ints = readLine()!.split(separator: " ").map { Int($0)! }
    return (a: ints[0], b: ints[1])
}

enum NumError: Error {
    case invalid(String)
}

extension NumError: LocalizedError {
    var errorDescription: String? {
        switch self {
        case .invalid(let errorMessage):
            return errorMessage
        }
    }
}

func caddi2018b_aa() throws -> Int {
    let N = readInt()
    
    if N < 999 {
        throw NumError.invalid("1000から9999の値を入れてください")
    }
    
    if N >= 10000 {
        throw NumError.invalid("1000から9999の値を入れてください")
    }
    
    return String(N).map {$0}.filter {$0 == "2"}.count
}

func caddi2018b_a() -> Result<Int, NumError> {
    let N = readInt()
    
    if N < 999 {
        return .failure(.invalid("1000から9999の値を入れてください"))
    }
    
    if N >= 10000 {
        return .failure(.invalid("1000から9999の値を入れてください"))
    }
    
    return .success(String(N).map {$0}.filter {$0 == "2"}.count)
}

let ans = caddi2018b_a()
switch ans {
case .failure(let error):
    print(error.localizedDescription)
case .success(let ans):
    print(ans)
}

//do {
//    try print(caddi2018b_a())
//} catch NumError.invalid(let errorMessage) {
//    print(errorMessage)
//}
