# [Perl 5], 90 + 1 (-p) = 91 bytes

<!-- language-all: lang-perl -->

    @a=0..3*$_;$_=(map{@a[$d,$s]=@a[$s=$a[$_]+$a[$_-1],$d=abs$a[$_]-$a[$_-1]];$a[1]}1..$_)[-1]

[Try it online!][TIO-j62oy7an]

[Perl 5]: https://www.perl.org/
[TIO-j62oy7an]: https://tio.run/##K0gtyjH9/98h0dZAT89YSyXeWiXeViM3saDaITFaJUVHpTjWFsQqtlUBkvGx2mBK1zBWRyXFNjGpGCKqCxONtQayDGNrDfX0VOI1o4Ei//8bchlxGXOZcJlyGRkY/MsvKMnMzyv@r1uQAwA "Perl 5 – Try It Online"