This should get you close, though it assumes the two input files have the same number of lines.
use strict;
use warnings;
my @f1 = get_lines("1.txt");
my @f2 = get_lines("2.txt");
open(my $out1, ">", "3.txt") or die;
open(my $out2, ">", "4.txt") or die;
for my $i (0..$#f1){
if (abs($f1[$i]->[1]-$f2[$i]->[1])>1){
print $out1 "$f1[$i]->[0]\n";
print $out2 "$f2[$i]->[0]\n";
}
}
close $out1;
close $out2;
sub get_lines {
my $file = shift;
my @lines;
open(my $fh, "<", $file) or die("Unable to open $file: $!");
while(<$fh>){
chomp;
/(\d+)$/;
push @lines, [ $_, $1 ];
}
close $fh;
return @lines;
}