Scanf.scanf "%d %s" (fun n s ->
    let e = Array.make n 0 in
    let w = Array.make n 0 in
    if s.[0] = 'E' then e.(0) <- 1 else w.(0) <- 1;
    for j = 1 to n - 1 do
        if s.[j] = 'E' then (e.(j) <- e.(j - 1) + 1; w.(j) <- w.(j - 1))
                       else (w.(j) <- w.(j - 1) + 1; e.(j) <- e.(j - 1))
    done;
    let rec loop i acc =
        if i = n then acc else (
            let q =
                if i = 0     then e.(n - 1) - e.(i) else
                if i = n - 1 then w.(i - 1)         else
                                  e.(n - 1) - e.(i) + w.(i - 1)
            in
            let acc = min acc q in
            loop (i + 1) acc

        )
    in
    loop 0 max_int |> Printf.printf "%d\n"
)