Language: C++

use v5.18; # strict say state
use warnings;
use List::Util qw(reduce first max min sum0);

chomp (my ($n, $m) = split / /, <STDIN>);
chomp (my $s = <STDIN>);
chomp (my $t = <STDIN>);
my $gcd = sub {
  my ($x, $y) = @_;
  while ($y) {
    my $r = $x % $y;
    $r += $y if $r < 0;
    $x = $y;
    $y = $r;
  }
  return $x;
};
my $lcm = sub {
  my ($x, $y) = @_;
  $x / $gcd->($x, $y) * $y;
};
my $ans = $lcm->($n, $m);
my $f = 1;
my (@xn, @xm);
for my $i (0..$ans-1) {
  $xn[$i] = "0";
  $xm[$i] = "0";
}
$xn[0] = substr($s, 0, 1);
$xm[0] = substr($t, 0, 1);
for my $i (1..$n-1) {
  $xn[$i * $ans / $n] = substr($s, $i, 1);
}
for my $i (1..$m-1) {
  $xm[$i * $ans / $m] = substr($t, $i, 1);
}
for my $i (0..$ans-1) {
  $f = 0 if $xn[$i] ne "0" && $xm[$i] ne "0" && $xn[$i] ne $xm[$i];
}
say $ans if $f == 1;
say -1 unless $f == 1;

Compiler output:
a.cc:1:12: error: stray '#' in program
    1 | use v5.18; # strict say state
      |            ^
a.cc:9:17: error: stray '@' in program
    9 |   my ($x, $y) = @_;
      |                 ^
a.cc:19:17: error: stray '@' in program
   19 |   my ($x, $y) = @_;
      |                 ^
a.cc:24:5: error: stray '@' in program
   24 | my (@xn, @xm);
      |     ^
a.cc:24:10: error: stray '@' in program
   24 | my (@xn, @xm);
      |          ^
a.cc:25:12: error: too many decimal points in number
   25 | for my $i (0..$ans-1) {
      |            ^~~~~~~
a.cc:31:12: error: too many decimal points in number
   31 | for my $i (1..$n-1) {
      |            ^~~~~
a.cc:34:12: error: too many decimal points in number
   34 | for my $i (1..$m-1) {
      |            ^~~~~
a.cc:37:12: error: too many decimal points in number
   37 | for my $i (0..$ans-1) {
      |            ^~~~~~~
a.cc:1:1: error: 'use' does not name a type
    1 | use v5.18; # strict say state
      | ^~~
a.cc:1:14: error: 'strict' does not name a type; did you mean 'struct'?
    1 | use v5.18; # strict say state
      |              ^~~~~~
      |              struct
a.cc:3:1: error: 'use' does not name a type
    3 | use List::Util qw(reduce first max min sum0);
      | ^~~
a.cc:5:7: error: expected constructor, destructor, or type conversion before '(' token
    5 | chomp (my ($n, $m) = split / /, <STDIN>);
      |       ^
a.cc:6:7: error: expected constructor, destructor, or type conversion before '(' token
    6 | chomp (my $s = <STDIN>);
      |       ^
a.cc:7:7: error: expected constructor, destructor, or type conversion before '(' token
    7 | chomp (my $t = <STDIN>);
      |       ^
a.cc:8:1: error: 'my' does not name a type
    8 | my $gcd = sub {
      | ^~
a.cc:18:1: error: 'my' does not name a type
   18 | my $lcm = sub {
      | ^~
a.cc:22:1: error: 'my' does not name a type
   22 | my $ans = $lcm->($n, $m);
      | ^~
a.cc:23:1: error: 'my' does not name a type
   23 | my $f = 1;
      | ^~
a.cc:24:4: error: expected constructor, destructor, or type conversion before '(' token
   24 | my (@xn, @xm);
      |    ^
a.cc:25:1: error: expected unqualified-id before 'for'
   25 | for my $i (0..$ans-1) {
      | ^~~
a.cc:29:1: error: '$xn' does not name a type
   29 | $xn[0] = substr($s, 0, 1);
      | ^~~
a.cc:30:1: error: '$xm' does not name a type
   30 | $xm[0] = substr($t, 0, 1);
      | ^~~
a.cc:31:1: error: expected unqualified-id before 'for'
   31 | for my $i (1..$n-1) {
      | ^~~
a.cc:34:1: error: expected unqualified-id before 'for'
   34 | for my $i (1..$m-1) {
      | ^~~
a.cc:37:1: error: expected unqualified-id before 'for'
   37 | for my $i (0..$ans-1) {
      | ^~~
a.cc:40:1: error: 'say' does not name a type
   40 | say $ans if $f == 1;
      | ^~~
a.cc:41:1: error: 'say' does not name a type
   41 | say -1 unless $f == 1;
      | ^~~