Scanf.scanf "%s %s" (fun s t ->
    let m = String.length s in
    let n = String.length t in
    let h = Array.make 26 [] in
    String.iteri (fun i c ->
        let c = int_of_char c - 97 in
        h.(c) <- i :: h.(c)) s;
    let h = Array.map (fun l -> Array.of_list (List.rev l)) h in

    let find a p =
        let ll = Array.length a in
        let rec loop l r =
            if l = r then (if l = ll then (-1) else a.(l)) else
                let m = (l + r) / 2 in
                if a.(m) < p then loop (m + 1) r else loop l m
        in
        loop 0 ll
    in

    let rec loop i cur acc =
        if i = n then m * acc + cur else
            let q = int_of_char t.[i] - 97 in
            let idx = find h.(q) cur in
            if idx >= 0 then loop (i + 1) (idx + 1) acc else
                let idx = find h.(q) 0 in
                if idx >= 0 then loop (i + 1) (idx + 1) (acc + 1) else
                    -1
    in
    loop 0 0 0 |> Printf.printf "%d\n"
)