← Home
```go
package main

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

type Item struct {
	q int
	p int
}

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

	const maxCapacity = 10 * 1024 * 1024
	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)

	if !scanner.Scan() {
		return
	}
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	k := make([]int, n)
	seqs := make([][]Item, m+1)

	for i := 0; i < n; i++ {
		scanner.Scan()
		ki, _ := strconv.Atoi(scanner.Text())
		k[i] = ki
		for j := 0; j < ki; j++ {
			scanner.Scan()
			val, _ := strconv.Atoi(scanner.Text())
			seqs[val] = append(seqs[val], Item{q: i, p: j})
		}
	}

	allPrimePowers := []int{2, 4, 8, 16, 32, 3, 9, 27, 5, 25, 7, 11, 13, 17, 19, 23, 29, 31, 37}
	primePowers := make([][]int, 41)
	for M := 1; M <= 40; M++ {
		for _, pp := range allPrimePowers {
			if M%pp == 0 {
				primePowers[M] = append(primePowers[M], pp)
			}
		}
	}

	ans := make([]int, m+1)

	var residueCount [40][40]int
	var distinctResidues [40]int
	var inconsistentPCount int

	add := func(M, V int) {
		for _, P := range primePowers[M] {
			res := V % P
			if residueCount[P][res] == 0 {
				distinctResidues[P]++
				if distinctResidues[P] == 2 {
					inconsistentPCount++
				}
			}
			residueCount[P][res]++
		}
	}

	remove := func(M, V int) {
		for _, P := range primePowers[M] {
			res := V % P
			residueCount[P][res]--
			if residueCount[P][res] == 0 {
				distinctResidues[P]--
				if distinctResidues[P] == 1 {
					inconsistentPCount--
				}
			}
		}
	}

	for x := 1; x <= m; x++ {
		c := len(seqs[x])
		if c == 0 {
			ans[x] = 0
			continue
		}

		validJ := make([]int, 0, 3*c)
		for iter := 0; iter < 3; iter++ {
			for _, item := range seqs[x] {
				validJ = append(validJ, item.q+iter*n)
			}
		}

		maxL := 0
		i := 0
		for i < len(validJ) {
			j := i
			for j+1 < len(validJ) && validJ[j+1] == validJ[j]+1 {
				j++
			}

			blockLen := j - i + 1
			left := 0
			for right := 0; right < blockLen; right++ {
				globalIdxRight := i + right
				itemRight := seqs[x][globalIdxRight%c]
				qR := itemRight.q
				dR := validJ[globalIdxRight] / n
				MR := k[qR]
				VR := (itemRight.p - dR) % MR
				if VR < 0 {
					VR += MR
				}
				add(MR, VR)

				for inconsistentPCount > 0 {
					globalIdxLeft := i + left
					itemLeft := seqs[x][globalIdxLeft%c]
					qL := itemLeft.q
					dL := validJ[globalIdxLeft] / n
					ML := k[qL]
					VL := (itemLeft.p - dL) % ML
					if VL < 0 {
						VL += ML
					}
					remove(ML, VL)
					left++
				}

				if right-left+1 > maxL {
					maxL = right - left + 1
				}
			}

			for left < blockLen {
				globalIdxLeft := i + left
				itemLeft := seqs[x][globalIdxLeft%c]
				qL := itemLeft.q
				dL := validJ[globalIdxLeft] / n
				ML := k[qL]
				VL := (itemLeft.p - dL) % ML
				if VL < 0 {
					VL += ML
				}
				remove(ML, VL)
				left++
			}

			i = j + 1
		}

		if maxL == 3*n {
			ans[x] = -1
		} else {
			ans[x] = maxL
		}
	}

	out := make([]string, m)
	zeros := strings.Repeat("0", 100)
	for x := 1; x <= m; x++ {
		if ans[x] == -1 {
			out[x-1] = strconv.Itoa(n) + zeros
		} else {
			out[x-1] = strconv.Itoa(ans[x])
		}
	}

	fmt.Println(strings.Join(out, " "))
}
```