Language: Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrlongWriter;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.ArrayList;
 
public class Main {
	public static void main(String args[])throws IOException  {
 
	
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrlongWriter out = new PrlongWriter(outputStream);
		long ans = -1;
		long n=in.nextlong();
		long p=in.nextlong();
		String s = in.nextString();
		String t = in.nextString();

		
		ans = findAns(n,p,s,t);
		
		//ans = findAns(n,p,out);

		out.prlong(ans);
		out.close();
	}

	static long findAns(long n,long p, String s, String t){
		long ans = -1;
		boolean findSame = true;		
		long gcd = gcd(n,p);
		long lcm = n*p/gcd;
		
		n=n/gcd;
		p=p/gcd;
		for(long i=0; i<gcd;i++){
			if(s.charAt(n*i) != t.charAt(p*i)){
				findSame = false;
				break;
			}
		}
		if(findSame)
			ans = lcm;
		else
			ans = -1;		
		
		return ans;
	}
	
    static long gcd(long a, long b) 
    { 
        return b>0?gcd(b, a%b):a; 
    } 

    static long lcm(long a, long b) 
    { 
        return (a*b)/gcd(a, b); 
    } 

	
	
	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;
 
		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}
 
		public long nextlong() {
			return longeger.parselong(nextString());
		}
 
		public long nextLong() {
			return Long.parseLong(nextString());
		}
 
		public double nextDouble() {
			return Double.parseDouble(nextString());
		}
 
		public long[] nextlongArray(long n) {
			long[] res = new long[n];
			for (long i = 0; i < n; i++) {
				res[i] = nextlong();
			}
			return res;
		}
 
		public long[] nextlongArrayDec(long n) {
			long[] res = new long[n];
			for (long i = 0; i < n; i++) {
				res[i] = nextlong() - 1;
			}
			return res;
		}
 
		public long[] nextlongArray1Index(long n) {
			long[] res = new long[n + 1];
			for (long i = 0; i < n; i++) {
				res[i + 1] = nextlong();
			}
			return res;
		}
 
		public long[] nextLongArray(long n) {
			long[] res = new long[n];
			for (long i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}
 
		public long[] nextLongArrayDec(long n) {
			long[] res = new long[n];
			for (long i = 0; i < n; i++) {
				res[i] = nextLong() - 1;
			}
			return res;
		}
 
		public long[] nextLongArray1Index(long n) {
			long[] res = new long[n + 1];
			for (long i = 0; i < n; i++) {
				res[i + 1] = nextLong();
			}
			return res;
		}
 
		public double[] nextDoubleArray(long n) {
			double[] res = new double[n];
			for (long i = 0; i < n; i++) {
				res[i] = nextDouble();
			}
			return res;
		}
 
		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}
	}
 
	
}


Compiler output:
Main.java:6: error: cannot find symbol
import java.io.PrlongWriter;
              ^
  symbol:   class PrlongWriter
  location: package java.io
Main.java:19: error: cannot find symbol
		PrlongWriter out = new PrlongWriter(outputStream);
		^
  symbol:   class PrlongWriter
  location: class Main
Main.java:19: error: cannot find symbol
		PrlongWriter out = new PrlongWriter(outputStream);
		                       ^
  symbol:   class PrlongWriter
  location: class Main
Main.java:44: error: incompatible types: possible lossy conversion from long to int
			if(s.charAt(n*i) != t.charAt(p*i)){
			             ^
Main.java:44: error: incompatible types: possible lossy conversion from long to int
			if(s.charAt(n*i) != t.charAt(p*i)){
			                              ^
Main.java:85: error: cannot find symbol
			return longeger.parselong(nextString());
			       ^
  symbol:   variable longeger
  location: class InputReader
Main.java:97: error: incompatible types: possible lossy conversion from long to int
			long[] res = new long[n];
			                      ^
Main.java:99: error: incompatible types: possible lossy conversion from long to int
				res[i] = nextlong();
				    ^
Main.java:105: error: incompatible types: possible lossy conversion from long to int
			long[] res = new long[n];
			                      ^
Main.java:107: error: incompatible types: possible lossy conversion from long to int
				res[i] = nextlong() - 1;
				    ^
Main.java:113: error: incompatible types: possible lossy conversion from long to int
			long[] res = new long[n + 1];
			                        ^
Main.java:115: error: incompatible types: possible lossy conversion from long to int
				res[i + 1] = nextlong();
				      ^
Main.java:121: error: incompatible types: possible lossy conversion from long to int
			long[] res = new long[n];
			                      ^
Main.java:123: error: incompatible types: possible lossy conversion from long to int
				res[i] = nextLong();
				    ^
Main.java:129: error: incompatible types: possible lossy conversion from long to int
			long[] res = new long[n];
			                      ^
Main.java:131: error: incompatible types: possible lossy conversion from long to int
				res[i] = nextLong() - 1;
				    ^
Main.java:137: error: incompatible types: possible lossy conversion from long to int
			long[] res = new long[n + 1];
			                        ^
Main.java:139: error: incompatible types: possible lossy conversion from long to int
				res[i + 1] = nextLong();
				      ^
Main.java:145: error: incompatible types: possible lossy conversion from long to int
			double[] res = new double[n];
			                          ^
Main.java:147: error: incompatible types: possible lossy conversion from long to int
				res[i] = nextDouble();
				    ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
20 errors