Scanf.scanf "%d %s" (fun n s ->
    let rec loop i r g b rg rb gb rgb =
        if i = n then rgb else
            match s.[i] with
            | 'R' -> loop (i + 1) (r + 1) g b (rg + g) (rb + b) gb (rgb + gb)
            | 'G' -> loop (i + 1) r (g + 1) b (rg + r) rb (gb + b) (rgb + rb)
            | _   -> loop (i + 1) r g (b + 1) rg (rb + r) (gb + g) (rgb + rg)
    in
    let acc = loop 0 0 0 0 0 0 0 0 in

    let rec loop_i i acc =
        let rec loop_j j acc =
            if j >= n then acc else
                let acc = if s.[i] = s.[j] then acc else
                    let k = j - i + j in
                    if k >= n then acc else
                        if s.[i] <> s.[k] && s.[j] <> s.[k] then acc - 1 else acc
                in
                loop_j (j + 1) acc
        in
        if i = n then acc else loop_i (i + 1) (loop_j (i + 1) acc)
    in
    loop_i 0 acc |> Printf.printf "%d\n"
)