#!/usr/bin/perl -w

use v5.36;
use strict;
use experimental 'multidimensional';

my %seen = ("0,0" => 1);
my @knots;
push @knots, [0, 0] for 1 .. 10;

sub follow ($x, $y, $tx, $ty) {
	if ($x == $tx) {
		if ($y == $ty - 2) {
			$ty--;
		} elsif ($y == $ty + 2) {
			$ty++;
		}
	} elsif ($y == $ty) {
		if ($x == $tx - 2) {
			$tx--;
		} elsif ($x == $tx + 2) {
			$tx++;
		}
	} elsif ($x == $tx - 2) {
		$tx--;
		if ($y > $ty) {
			$ty++;
		} else {
			$ty--
		}
	} elsif ($x == $tx + 2) {
		$tx++;
		if ($y > $ty) {
			$ty++;
		} else {
			$ty--;
		}
	} elsif ($y == $ty - 2) {
		$ty--;
		if ($x > $tx) {
			$tx++;
		} else {
			$tx--;
		}
	} elsif ($y == $ty + 2) {
		$ty++;
		if ($x > $tx) {
			$tx++;
		} else {
			$tx--;
		}
	}
	($tx, $ty);
}

while (<>) {
	my ($dir, $len) = /^(.) (\d+)/;

	while ($len--) {
		if ($dir eq 'U') {
			$knots[0][1]--;
		} elsif ($dir eq 'D') {
			$knots[0][1]++;
		} elsif ($dir eq 'L') {
			$knots[0][0]--;
		} elsif ($dir eq 'R') {
			$knots[0][0]++;
		}
		
		for (1 .. $#knots) {
			($knots[$_][0], $knots[$_][1]) = follow(
				@{ $knots[$_-1] },
				@{ $knots[$_] }
			);
		}
	
		$seen{"$knots[-1][0],$knots[-1][1]"}++;
	}
}

say scalar keys %seen;

