#!/usr/bin/perl -w

use v5.16;

$; = ',';
sub walk {
	my ($desc) = @_;
	my %rv;
	my ($x, $y) = (0, 0);
	for my $step (split /,/, $desc) {
		my ($dir, $dist) = $step =~ /([A-Z])(\d+)/;
		my ($dx, $dy);
		($dx, $dy) = (1,  0) if $dir eq 'R';
		($dx, $dy) = (0,  1) if $dir eq 'D';
		($dx, $dy) = (0, -1) if $dir eq 'U';
		($dx, $dy) = (-1, 0) if $dir eq 'L';
		$rv{$x += $dx, $y += $dy} = 1 while $dist--;
		say "$step -> $x, $y";
	}
	return \%rv;
}

my $p1 = walk(scalar <>);
my $p2 = walk(scalar <>);

my $dist;
for my $pos (keys %$p1) {
	next if !$p2->{$pos};
	my ($x, $y) = split /,/, $pos;
	my $d = abs($x)+abs($y);
	$dist = $d if !$dist || $dist > $d;
}

say $dist;
	
