#!/usr/bin/perl use v5.35.0; use List::Util qw(sum); my @input = map [split], <>; my @left = map $$_[0], @input; my @right = map $$_[1], @input; say sum map {my $id = $_; grep $id == $_, @right} @left;