defmodule Main do def input, do: IO.read(:line) |> String.trim def ii, do: input() |> String.to_integer def li, do: input() |> String.split |> Enum.map(&String.to_integer/1) def yn(b), do: IO.puts(if b, do: "Yes", else: "No") def main do x = ii() s = Stream.cycle([2, 2, -1, -1, 2, -1, -1]) pred = fn {p, _d} -> p == x end next = fn {p, d}, x -> {p+x, d+abs(x)} end {_, d} = loop(s, {0, 0}, pred, next) IO.puts d end def loop(stream, acc, pred, next) do if pred.(acc) do acc else x = Enum.take(stream, 1) |> hd loop(Stream.drop(stream, 1), next.(acc, x), pred, next) end end end