← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func extendedGCD(a, b int64) (int64, int64, int64) {
	if a == 0 {
		return b, 0, 1
	}
	d, x1, y1 := extendedGCD(b%a, a)
	x := y1 - (b/a)*x1
	y := x1
	return d, x, y
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanInt64 := func() int64 {
		scanner.Scan()
		x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		return x
	}
	scanInt := func() int {
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		return x
	}

	if !scanner.Scan() {
		return
	}
	nStr := scanner.Text()
	n, _ := strconv.ParseInt(nStr, 10, 64)
	m := scanInt64()
	k := scanInt64()

	a := make([]int, n)
	for i := range a {
		a[i] = scanInt()
	}

	b := make([]int, m)
	for i := range b {
		b[i] = scanInt()
	}

	const maxColor = 1000005
	posA := make([]int, maxColor)
	for i := range posA {
		posA[i] = -1
	}
	for i, val := range a {
		posA[val] = i
	}

	g := gcd(n, m)
	L := (n / g) * m

	nPrime := n / g
	mPrime := m / g
	_, inv, _ := extendedGCD(nPrime, mPrime)
	inv = (inv%mPrime + mPrime) % mPrime

	var matches []int64

	for j, col := range b {
		if col >= maxColor {
			continue
		}
		i := posA[col]
		if i != -1 {
			if int64(i-j)%g == 0 {
				rhs := (int64(j) - int64(i)) / g
				rhs = (rhs%mPrime + mPrime) % mPrime
				t := (rhs * inv) % mPrime
				val := int64(i) + n*t
				matches = append(matches, val)
			}
		}
	}

	sort.Slice(matches, func(i, j int) bool { return matches[i] < matches[j] })
	numMatches := int64(len(matches))

	low := int64(1)
	high := int64(2e18) 
	ans := high

	for low <= high {
		mid := low + (high-low)/2

		fullCycles := mid / L
		rem := mid % L

		cnt := fullCycles * numMatches
		idx := sort.Search(len(matches), func(i int) bool {
			return matches[i] >= rem
		})
		cnt += int64(idx)

		diff := mid - cnt
		if diff >= k {
			ans = mid
			high = mid - 1
		} else {
			low = mid + 1
		}
	}

	fmt.Println(ans)
}
```