Imports System.Collections.Generic
Imports System.Console

Module Program
    Sub Main()
        Dim n As Long = NextLong()
        Dim m As Long = NextLong()
        Dim s As String = NextString()
        Dim t As String = NextString()
        Dim l As Long = LCM(n, m)
        Dim dic As New Dictionary(Of Long, Char)


        For i As Integer = 0 To Len(s) - 1
            Dim j As Long = i * l \ n
            dic(j) = s(i)
        Next

        For i As Integer = 0 To Len(t) - 1
            Dim j As Long = i * l \ m
            If dic.ContainsKey(j) AndAlso dic(j) <> t(i) Then
                WriteLine(-1)
                Return
            End If
        Next

        WriteLine(l)
    End Sub

    Function LCM(x As Long, y As Long) As Long
        Return x * y \ GCD(x, y)
    End Function

    Function GCD(x As Long, y As Long) As Long
        If x < y Then
            Return GCD(y, x)
        ElseIf y = 0 Then
            Return x
        Else
            Return GCD(y, x Mod y)
        End If
    End Function

    Function NextLong() As Long
        Return CLng(NextString())
    End Function

    Function NextString() As String
        Dim result As New List(Of Char)
        Do
            Dim tmp As Integer = Read()
            If tmp < 0 Then Exit Do
            Dim nextChar As Char = Chr(tmp)

            If Not Char.IsWhiteSpace(nextChar) Then
                result.Add(nextChar)
            ElseIf nextChar <> vbCr Then
                Exit Do
            End If
        Loop
        Return New String(result.ToArray())
    End Function
End Module
