use Algorithm::Permute 'permute';

print min_chunks(@ARGV);

sub min_chunks{
    my ($word1, $word2) = @_;
    my $string = "$word1:$word2";
    my $n = length $word1;

    for my $chunks (2 .. $n-1){
        my $begining_pattern = '([^:]+?)' x $chunks;
        $begining_pattern .= ":(?:";

        my @array = 1 .. $chunks;
        permute {
        $begining_pattern .= (join "", map {"\\$_"} @array)."|";
        } @array;
        chop $begining_pattern;
        return $chunks if $string =~ /^$begining_pattern)$/;
        
    }
    return $n;
}
