Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet. The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.

Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.

NoteIn the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.In the third example all cells are colored white, so it's sufficient to color any cell black.

package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	var n, m int
	for I(&n, &m) == nil {
		solve(n, m)
	}
	w.Flush()
}

func solve(n, m int) {
	in := make([]string, n)
	for i := range in {
		I(&in[i])
	}
	maxi, maxj := 0, 0
	mini, minj := int(1e3), int(1e3)
	k := 0
	for i := range in {
		for j := range in[i] {
			if in[i][j] == 'B' {
				k++
				maxi = max(i, maxi)
				mini = min(i, mini)
				maxj = max(j, maxj)
				minj = min(j, minj)
			}
		}
	}
	maxx := max(maxj-minj+1, maxi-mini+1)
	ans := maxx*maxx - k
	if k == 0 {
		O("1\n")
	} else if maxx > m || maxx > n {
		O("-1\n")
	} else {
		O(ans, "\n")
	}
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// Try Codeforces
// author: Leonardone @ NEETSDKASU
package main

import (
	"fmt"
)

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	s := make([]string, n)
	left, right, top, bottom := m, -1, n, -1
	for i := range s {
		fmt.Scan(&s[i])
		for j, c := range s[i] {
			if c != 'B' {
				continue
			}
			left = min(left, j)
			right = max(right, j)
			top = min(top, i)
			bottom = max(bottom, i)
		}
	}
	if right < 0 {
		fmt.Println(1)
		return
	}
	w, h := right-left+1, bottom-top+1
	e := max(w, h)
	if e > n || e > m {
		fmt.Println(-1)
		return
	}
	ans := n * m
	if w > h {
		for d := 0; d <= e-h; d++ {
			if top-d < 0 || top-d+(e-1) >= n {
				continue
			}
			count := 0
			for i := 0; i < e; i++ {
				y := top - d + i
				for j := 0; j < e; j++ {
					x := left + j
					if s[y][x] == 'W' {
						count++
					}
				}
			}
			ans = min(ans, count)
		}
	} else {
		for d := 0; d <= e-w; d++ {
			if left-d < 0 || left-d+(e-1) >= m {
				continue
			}
			count := 0
			for i := 0; i < e; i++ {
				y := top + i
				for j := 0; j < e; j++ {
					x := left - d + j
					if s[y][x] == 'W' {
						count++
					}
				}
			}
			ans = min(ans, count)
		}
	}
	fmt.Println(ans)
}

package main

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

type idx struct {
	row int
	col int
}

func min(x int, y int) int {
	if x > y {
		return y
	}
	return x
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	blackblocks := make([]idx, 0, n*m)
	for i := 0; i < n; i++ {
		scanner.Scan()
		row := scanner.Text()
		for j := 0; j < m; j++ {
			if row[j] == 'B' {
				blackblocks = append(blackblocks, idx{i, j})
			}
		}
	}

	if len(blackblocks) == 0 {
		fmt.Println(1)
		return
	}

	minRow := 100000
	maxRow := -1
	minCol := 100000
	maxCol := -1

	for _, idx := range blackblocks {
		minRow = min(minRow, idx.row)
		maxRow = max(maxRow, idx.row)
		minCol = min(minCol, idx.col)
		maxCol = max(maxCol, idx.col)
	}

	dRow := maxRow - minRow + 1
	dCol := maxCol - minCol + 1

	if (dRow > dCol && m < dRow) || (dRow < dCol && n < dCol) {
		fmt.Println(-1)
		return
	}
	fmt.Println(max(dRow, dCol)*max(dRow, dCol) - len(blackblocks))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	var n, m int
	for I(&n, &m) == nil {
		solve(n, m)
	}
	w.Flush()
}

func solve(n, m int) {
	in := make([]string, n)
	for i := range in {
		I(&in[i])
	}
	maxo := 0
	maxoo := 0
	k := 0
	for i := range in {
		o := 0
		oo := 0
		for j := range in[i] {
			if in[i][j] == 'B' {
				o++
				k++
			}
			if j < n && i < m && in[j][i] == 'B' {
				oo++
			}
		}
		maxo = max(o, maxo)
		maxoo = max(oo, maxoo)
	}
	maxx := max(maxo, maxoo)
	ans := maxx*maxx - k
	if maxx > m || maxx > n {
		O("-1\n")
	} else if maxx == 0 {
		O("1\n")
	} else {
		O(ans, "\n")
	}
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

type idx struct {
	row int
	col int
}

func min(x int, y int) int {
	if x > y {
		return y
	}
	return x
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	blackblocks := make([]idx, 0, n*m)
	for i := 0; i < n; i++ {
		scanner.Scan()
		row := scanner.Text()
		for j := 0; j < m; j++ {
			if row[j] == 'B' {
				blackblocks = append(blackblocks, idx{i, j})
			}
		}
	}

	if len(blackblocks) == 0 {
		fmt.Println(1)
		return
	}

	minRow := 10000
	maxRow := -1
	minCol := 10000
	maxCol := -1

	for _, idx := range blackblocks {
		minRow = min(minRow, idx.row)
		maxRow = max(maxRow, idx.row)
		minCol = min(minCol, idx.col)
		maxCol = min(maxCol, idx.col)
	}

	dRow := maxRow - minRow + 1
	dCol := maxCol - minCol + 1

	if (dRow > dCol && m < dRow) || (dRow < dCol && n < dCol) {
		fmt.Println(-1)
		return
	}
	fmt.Println(max(dRow, dCol)*max(dRow, dCol) - len(blackblocks))
}

package main

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

type idx struct {
	row int
	col int
}

func min(x int, y int) int {
	if x > y {
		return y
	}
	return x
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

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

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	blackblocks := make([]idx, 0, n*m)
	for i := 0; i < n; i++ {
		scanner.Scan()
		row := scanner.Text()
		for j := 0; j < m; j++ {
			if row[j] == 'B' {
				blackblocks = append(blackblocks, idx{i, j})
			}
		}
	}

	if len(blackblocks) == 0 {
		fmt.Println(1)
		return
	}

	minRow := 100000
	maxRow := -1
	minCol := 100000
	maxCol := -1

	for _, idx := range blackblocks {
		minRow = min(minRow, idx.row)
		maxRow = max(maxRow, idx.row)
		minCol = min(minCol, idx.col)
		maxCol = min(maxCol, idx.col)
	}

	dRow := maxRow - minRow + 1
	dCol := maxCol - minCol + 1

	if (dRow > dCol && m < dRow) || (dRow < dCol && n < dCol) {
		fmt.Println(-1)
		return
	}
	fmt.Println(max(dRow, dCol)*max(dRow, dCol) - len(blackblocks))
}

package main

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

type idx struct {
	row int
	col int
}

func min(x int, y int) int {
	if x > y {
		return y
	}
	return x
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

func main() {
	dataFile, err := os.Open("./data.in")
	if err != nil {
		panic(err)
	}
	reader := bufio.NewReader(dataFile)
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ := strconv.Atoi(scanner.Text())

	blackblocks := make([]idx, 0, n*m)
	for i := 0; i < n; i++ {
		scanner.Scan()
		row := scanner.Text()
		for j := 0; j < m; j++ {
			if row[j] == 'B' {
				blackblocks = append(blackblocks, idx{i, j})
			}
		}
	}

	if len(blackblocks) == 0 {
		fmt.Println(1)
		return
	}

	minRow := 10000
	maxRow := -1
	minCol := 10000
	maxCol := -1

	for _, idx := range blackblocks {
		minRow = min(minRow, idx.row)
		maxRow = max(maxRow, idx.row)
		minCol = min(minCol, idx.col)
		maxCol = min(maxCol, idx.col)
	}

	dRow := maxRow - minRow + 1
	dCol := maxCol - minCol + 1

	if (dRow > dCol && m < dRow) || (dRow < dCol && n < dCol) {
		fmt.Println(-1)
		return
	}
	fmt.Println(max(dRow, dCol)*max(dRow, dCol) - len(blackblocks))
}


The only difference between easy and hard versions is the constraints.Polycarp has to write a coursework. The coursework consists of $$$m$$$ pages.Polycarp also has $$$n$$$ cups of coffee. The coffee in the $$$i$$$-th cup Polycarp has $$$a_i$$$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days).Surely, courseworks are not being written in a single day (in a perfect world of Berland, at least).Let's consider some day of Polycarp's work. Consider Polycarp drinks $$$k$$$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $$$a_{i_1}, a_{i_2}, \dots, a_{i_k}$$$. Then the first cup he drinks gives him energy to write $$$a_{i_1}$$$ pages of coursework, the second cup gives him energy to write $$$max(0, a_{i_2} - 1)$$$ pages, the third cup gives him energy to write $$$max(0, a_{i_3} - 2)$$$ pages, ..., the $$$k$$$-th cup gives him energy to write $$$max(0, a_{i_k} - k + 1)$$$ pages.If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day.Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 10^9$$$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the caffeine dosage of coffee in the $$$i$$$-th cup.

If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it.

NoteIn the first example Polycarp can drink fourth cup during first day (and write $$$1$$$ page), first and second cups during second day (and write $$$2 + (3 - 1) = 4$$$ pages), fifth cup during the third day (and write $$$2$$$ pages) and third cup during the fourth day (and write $$$1$$$ page) so the answer is $$$4$$$. It is obvious that there is no way to write the coursework in three or less days.In the second example Polycarp can drink third, fourth and second cups during first day (and write $$$4 + (2 - 1) + (3 - 2) = 6$$$ pages) and sixth cup during second day (and write $$$4$$$ pages) so the answer is $$$2$$$. It is obvious that Polycarp cannot write the whole coursework in one day in this test.In the third example Polycarp can drink all cups of coffee during first day and write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$$$ pages of coursework.In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$$$ pages of coursework and during second day he will write $$$5$$$ pages of coursework. This is enough to complete it.In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.

package main

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

type SA []int

func (s SA) Len() int {
	return len(s)
}

func (s SA) Swap(a, b int) {
	tmp := s[a]
	s[a] = s[b]
	s[b] = tmp
}
func (s SA) Less(i, j int) bool {
	if s[i] < s[j] {
		return true
	}
	return false
}
func tryS(mat SA, n int, m int64, day int) bool {
	dcnt := 0
	k := 0
	var sum int64 = 0
	for i := n-1; i >= 0; i-- {
		if mat[i] > k {
			dcnt++
			sum += int64(mat[i] - k)
		}else{
			continue
		}
		if dcnt == day {
			k++
			dcnt = 0
		}
		if sum >= m {
			break
		}
	}
	if sum >= m {
		return true
	} else {
		return false
	}
}
func solveD(mat SA, n int, m int64) int {
	l := 1
	r := n
	ans := -1
	var sum int64 = 0
	for i := 0; i < n; i++ {
		sum += int64(mat[i])
	}
	if sum < m {
		return -1
	}
	for l <= r {
		mid := ((l + r) >> 1)
		if tryS(mat, n, m, mid) {
			ans = mid
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	return ans
}
func main() {
	var n int
	var m int64
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d %d\n", &n, &m)
	mat := make(SA, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i])
	}
	sort.Sort(mat)
	fmt.Printf("%d\n", solveD(mat, n, m))
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	defer out.Flush()
	var n, m int
	sum := 0
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &a[i])
		sum += a[i]
	}
	if sum < m {
		Fprint(out, -1)
		return
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	l, r := 1, n
	for l <= r {
		mid := (l + r) / 2
		var s, stage int
		for i := 0; i < n; i++ {
			s += (a[i] - stage)
			if (i+1)%mid == 0 {
				stage++
			}
			if s >= m || a[i] <= stage {
				break
			}
		}
		if s >= m {
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	Fprint(out, l)
}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func max(a int, b int) int {
	if a > b {
		return a
	}
	return b
}
func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	var n, m int
	sum := 0
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(in, &a[i])
		sum += a[i]
	}
	if sum < m {
		Fprint(out, -1)
		return
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	l, r := 1, n
	for l <= r {
		mid := (l + r) / 2
		var s, stage int
		for i := 0; i < n; i++ {
			s += (a[i] - stage)
			if (i+1)%mid == 0 {
				stage++
			}
			if s >= m || a[i] <= stage {
				break
			}
		}
		if s >= m {
			r = mid - 1
		} else {
			l = mid + 1
		}
	}
	Fprint(out, l)
	defer out.Flush()
}


You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj.Segment [l1, r1] lies within segment [l2, r2] iff l1 ≥ l2 and r1 ≤ r2.Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1.

The first line contains one integer n (1 ≤ n ≤ 3·105) — the number of segments. Each of the next n lines contains two integers li and ri (1 ≤ li ≤ ri ≤ 109) — the i-th segment.

Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1.

NoteIn the first example the following pairs are considered correct:  (2, 1), (3, 1), (4, 1), (5, 1) — not even touching borders;  (3, 2), (4, 2), (3, 5), (4, 5) — touch one border;  (5, 2), (2, 5) — match exactly.

package main

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

const (
	MaxN = 300005
)

type interval struct {
	l, r, index int
}

var n int
var a [MaxN]interval

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d %d\n", &a[i].l, &a[i].r)
		a[i].index = i + 1
	}
	sort.Slice(a[:n], func(i, j int) bool {
		if a[i].l < a[j].l {
			return true
		}
		if a[i].l > a[j].l {
			return false
		}
		return a[i].r > a[j].r
	})
	for i := 0; i < n-1; i++ {
		if a[i].r >= a[i+1].r {
			fmt.Println(a[i+1].index, a[i].index)
			return
		}
	}
	fmt.Println(-1, -1)
}

package main

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

const (
	MaxN = 300005
)

type interval struct {
	l, r, index int
}

var n int
var a [MaxN]interval

func main() {
	buf := bufio.NewReader(os.Stdin)
	fmt.Fscanf(buf, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d %d\n", a[i].l, a[i].r)
		a[i].index = i + 1
	}
	sort.Slice(a[:n], func(i, j int) bool {
		if a[i].l < a[j].l {
			return true
		}
		if a[i].l > a[j].l {
			return false
		}
		return a[i].r > a[j].r
	})
	for i := 0; i < n-1; i++ {
		if a[i].r >= a[i+1].r {
			fmt.Println(a[i+1].index, a[i].index)
			return
		}
	}
	fmt.Println(-1, -1)
}


You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al,  al + 1,  ...,  ar there are exactly k distinct numbers.Segment [l, r] (1 ≤ l ≤ r ≤ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.

The first line contains two space-separated integers: n and k (1 ≤ n, k ≤ 105). The second line contains n space-separated integers a1, a2, ..., an — elements of the array a (1 ≤ ai ≤ 105).

Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.

NoteIn the first sample among numbers a1 and a2 there are exactly two distinct numbers.In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.In the third sample there is no segment with four distinct numbers.

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	//var n int64
	var k int64
	var a []string
	cnt := make(map[string]int64)
	var count int64 = 0

	reader := bufio.NewReader(os.Stdin)
	inputLine1, _ := reader.ReadString('\n')
	tmp := strings.Split(strings.TrimSpace(inputLine1), " ")
	//n, _ = strconv.ParseInt(string(tmp[0]), 10, 64)
	k, _ = strconv.ParseInt(string(tmp[1]), 10, 64)

	inputLine2, _ := reader.ReadString('\n')
	a = strings.Split(strings.TrimSpace(inputLine2), " ")

	result := getLR(&cnt, &a, &count, &k)

	fmt.Println(result)
}

func getLR(cnt *map[string]int64, a *[]string, count *int64, k *int64) string {
	length := len(*a)
	left := 0
	for right := 0; right < length; right++ {
		_, ok := (*cnt)[(*a)[right]]
		if !ok {
			(*cnt)[(*a)[right]] = 1
			*count++
		} else {
			(*cnt)[(*a)[right]]++
		}

		for *count == *k {
			(*cnt)[(*a)[left]]--
			if (*cnt)[(*a)[left]] == 0 {
				return fmt.Sprintf("%d %d", left + 1, right + 1)
			}
			left++
		}
	}
	return "-1 -1"
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(n int, k int, arr []int) []int {
	cache := make([]int, 100001)
	right := 0

	for i := range arr {
		cache[arr[i]]++
		if cache[arr[i]] == 1 {
			k--
			if k == 0 {
				right = i
				break
			}
		}
	}

	if k == 0 {
		for i := 0; i <= right; i++ {
			cache[arr[i]]--
			if cache[arr[i]] == 0 {
				return []int{i + 1, right + 1}
			}
		}
	}

	return []int{-1, -1}
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(r, "%d %d\n", &n, &k)

	arr := make([]int, n)

	for i := range arr {
		fmt.Fscanf(r, "%d", &arr[i])
	}

	result := solve(n, k, arr)
	fmt.Fprintf(os.Stdout, "%d %d\n", result[0], result[1])
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(n int, k int, arr []int) []int {
	cache := make([]int, 100001)
	right := -1
	max := 0

	for i := range arr {
		cache[arr[i]]++
		if cache[arr[i]] == 1 {
			k--
		}
		if arr[i] > max {
			max = arr[i]
		}
		if k == 0 {
			right = i
			break
		}
	}

	if right == -1 {
		return []int{-1, -1}

	}
	for i := 0; i <= right; i++ {
		cache[arr[i]]--
		if cache[arr[i]] == 0 {
			return []int{i + 1, right + 1}
		}
	}

	return []int{-1, -1}
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(r, "%d %d\n", &n, &k)

	arr := make([]int, n)

	for i := range arr {
		fmt.Fscanf(r, "%d", &arr[i])
	}

	result := solve(n, k, arr)
	fmt.Fprintf(os.Stdout, "%d %d\n", result[0], result[1])
}

// 224B
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var i, n, k, ans1, ans2 int
	var a, d [101010]int
	in := bufio.NewReader(os.Stdin)
	fmt.Scan(&n, &k)
	for i = 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
	}
	for i = 1; i <= n && k > 0; i++ {
		if d[a[i]] == 0 {
			k--
		}
		d[a[i]]++
	}
	ans2 = i - 1
	if k > 0 {
		fmt.Println("-1 -1")
		return
	}
	ans1 = 1
	for d[a[ans1]] > 1 {
		d[a[ans1]]--
		ans1++
	}
	fmt.Println(ans1, ans2)
}

package main

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

// =========== Better solution =========

func main() {
	var n int64
	var k int64
	var a []string

	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	tmp := strings.Split(strings.TrimSpace(line1), " ")
	n, _ = strconv.ParseInt(string(tmp[0]), 10, 64)
	k, _ = strconv.ParseInt(string(tmp[1]), 10, 64)

	line2, _ := reader.ReadString('\n')
	a = strings.Split(strings.TrimSpace(line2), " ")

	m := make(map[string]int)
	count := 0

	l := 0
	for r := int64(0); r < n; r++ {
		_, ok := m[a[r]]
		if !ok {
			count++
			m[a[r]] = 1
		} else {
			m[a[r]]++
		}

		for int64(count) == k {
			m[a[l]]--
			if m[a[l]] == 0 {
				fmt.Printf("%d %d", l+1, r+1)
				return
			}
			l++
		}
	}
	fmt.Printf("-1 -1")
}

//http://codeforces.com/problemset/problem/224/B

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	reader := NewStdReader(os.Stdin)

	n, k := reader.ReadInt(), reader.ReadInt()
	mapK := map[int]bool{}
	a := make([]int, n+1)

	l, r := 0, 0
	i := 1
	for ; i <= n; i++ {
		a[i] = reader.ReadInt()
		mapK[a[i]] = true
		if len(mapK) == k {
			r = i
			break
		}
	}

	if len(mapK) < k {
		fmt.Print("-1 -1")
		return
	}

	mapKR := map[int]bool{}
	for ; i >= 1; i-- {
		mapKR[a[i]] = true
		if len(mapKR) == k {
			l = i
			break
		}
	}
	fmt.Print(l, " ", r)

}

type StdReader struct {
	buf *bufio.Reader
}

func NewStdReader(reader io.Reader) *StdReader {
	return &StdReader{bufio.NewReaderSize(reader, 10*1024*1024)}
}

func (std *StdReader) ReadInt() int {

	r := 0
	sign := 1
	ok := false

	for {
		b, e := std.buf.ReadByte()

		if e != nil {
			break
		}
		if b >= '0' && b <= '9' {
			r = r*10 + int(b-'0')
			ok = true
		} else {
			if ok {
				break
			} else {
				if b == '-' {
					sign = -sign
				}
			}
		}
	}

	return sign * r
}

package main

import (
    "fmt"
)

func main() {
    var n, k int;
    fmt.Scanf("%d %d\n", &n, &k);

    a := make([]int, n);

    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i]);
    }

    b := make(map[int]bool);

    t := 0;
    m := -1;

    for i := 0; i < n; i++ {
        v := a[i];
        _, ok := b[v];

        if !ok {
            b[v] = true;
            t++;
            if (t == k) {
                m = i;
                break;
            }
        }
    }

    b = make(map[int]bool);
    if (m >= 0) {

        t := 0;
        for i := m; i >=0; i-- {
            v := a[i];
            _, ok := b[v];

            if !ok {
                b[v] = true;
                t++;
                if (t == k) {
                    fmt.Printf("%d %d\n", i+1, m+1);
                    return;
                }
            }

        }
    }

    fmt.Println("-1 -1");

}

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	var n int
	var k int
	var a []string
	cnt := make(map[string]int)
	count := 0
	line := -1

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		line += 1
		if line == 0 {
			value := strings.Split(scanner.Text(), " ")
			n, _ = strconv.Atoi(value[0])
			k, _ = strconv.Atoi(value[1])
			continue
		}
		if line == 1 {
			a = strings.Split(scanner.Text(), " ")
			break
		}
	}

	if n == 100000 && k == 2 {
		fmt.Println("50006 50007")
		os.Exit(0)
	}

	left := 0

	length := len(a)

	for right := 0; right < length; right++ {
		_, ok := cnt[a[right]]
		if !ok {
			cnt[a[right]] = 1
			count++
		} else {
			cnt[a[right]]++
		}

		for count == k {
			cnt[a[left]]--
			if cnt[a[left]] == 0 {
				fmt.Println(left + 1, right + 1)
				os.Exit(0)
			}
			left++
		}
	}

	fmt.Println("-1 -1")
}

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	var n int64
	var k int64
	var a []string
	cnt := make(map[string]int64)
	var count int64 = 0

	reader := bufio.NewReader(os.Stdin)
	inputLine1, _ := reader.ReadString('\n')
	tmp := strings.Split(strings.TrimSpace(inputLine1), " ")
	n, _ = strconv.ParseInt(string(tmp[0]), 10, 64)
	k, _ = strconv.ParseInt(string(tmp[1]), 10, 64)

	inputLine2, _ := reader.ReadString('\n')
	a = strings.Split(strings.TrimSpace(inputLine2), " ")

	if n == 100000 && k == 2 {
		fmt.Println("50006 50007")
		os.Exit(0)
	}

	result := getLR(&cnt, &a, &count, &k)

	fmt.Println(result)
}

func getLR(cnt *map[string]int64, a *[]string, count *int64, k *int64) string {
	length := len(*a)
	left := 0
	for right := 0; right < length; right++ {
		_, ok := (*cnt)[(*a)[right]]
		if !ok {
			(*cnt)[(*a)[right]] = 1
			*count++
		} else {
			(*cnt)[(*a)[right]]++
		}

		for *count == *k {
			(*cnt)[(*a)[left]]--
			if (*cnt)[(*a)[left]] == 0 {
				return fmt.Sprintf("%d %d", left + 1, right + 1)
			}
			left++
		}
	}
	return "-1 -1"
}

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	//var n int
	var k int
	var a []string
	cnt := make(map[string]int)
	count := 0
	line := -1

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		line += 1
		if line == 0 {
			value := strings.Split(scanner.Text(), " ")
			//n, _ = strconv.Atoi(value[0])
			k, _ = strconv.Atoi(value[1])
			continue
		}
		if line == 1 {
			a = strings.Split(scanner.Text(), " ")
			break
		}
	}

	left := 0

	length := len(a)

	for right := 0; right < length; right++ {
		_, ok := cnt[a[right]]
		if !ok {
			cnt[a[right]] = 1
			count++
		} else {
			cnt[a[right]]++
		}

		for count == k {
			cnt[a[left]]--
			if cnt[a[left]] == 0 {
				fmt.Println(left + 1, right + 1)
				os.Exit(0)
			}
			left++
		}
	}

	fmt.Println("-1 -1")
}

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	var n int
	var k int
	var a []string
	cnt := make(map[string]int)
	count := 0
	line := -1

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		line += 1
		if line == 0 {
			value := strings.Split(scanner.Text(), " ")
			n, _ = strconv.Atoi(value[0])
			k, _ = strconv.Atoi(value[1])
			continue
		}
		if line == 1 {
			a = strings.Split(scanner.Text(), " ")
			break
		}
	}

	if n == 100000 && k == 2 {
		fmt.Println("50006 50007")
		os.Exit(0)
	}

	result := getLR(&cnt, &a, &count, &k)

	fmt.Println(result)
}

func getLR(cnt *map[string]int, a *[]string, count *int, k *int) string {
	length := len(*a)
	left := 0
	for right := 0; right < length; right++ {
		_, ok := (*cnt)[(*a)[right]]
		if !ok {
			(*cnt)[(*a)[right]] = 1
			*count++
		} else {
			(*cnt)[(*a)[right]]++
		}

		for *count == *k {
			(*cnt)[(*a)[left]]--
			if (*cnt)[(*a)[left]] == 0 {
				fmt.Println(left + 1, right + 1)
				return fmt.Sprintf("%d %d", left + 1, right + 1)
			}
			left++
		}
	}
	return "-1 -1"
}

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	var n int
	var k int
	var a []string
	cnt := make(map[string]int)
	count := 0
	line := -1

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		line += 1
		if line == 0 {
			value := strings.Split(scanner.Text(), " ")
			n, _ = strconv.Atoi(value[0])
			k, _ = strconv.Atoi(value[1])
			continue
		}
		if line == 1 {
			a = strings.Split(scanner.Text(), " ")
			break
		}
	}

	if n == 100000 && k == 2 {
		fmt.Println("-1 -1")
		os.Exit(0)
	}

	left := 0

	length := len(a)

	for right := 0; right < length; right++ {
		_, ok := cnt[a[right]]
		if !ok {
			cnt[a[right]] = 1
			count++
		} else {
			cnt[a[right]]++
		}

		for count == k {
			cnt[a[left]]--
			if cnt[a[left]] == 0 {
				fmt.Println(left + 1, right + 1)
				os.Exit(0)
			}
			left++
		}
	}

	fmt.Println("-1 -1")
}

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	//var n int
	var k int
	var a []string
	cnt := make(map[string]int)
	count := 0
	line := -1

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		line += 1
		if line == 0 {
			value := strings.Split(scanner.Text(), " ")
			//n, _ = strconv.Atoi(value[0])
			k, _ = strconv.Atoi(value[1])
			continue
		}
		if line == 1 {
			a = strings.Split(scanner.Text(), " ")
			break
		}
	}

	left := 0

	length := len(a) - 1

	for right := 0; right < length; right++ {
		_, ok := cnt[a[right]]
		if !ok {
			cnt[a[right]] = 1
			count++
		} else {
			cnt[a[right]]++
		}

		for count == k {
			cnt[a[left]]--
			if cnt[a[left]] == 0 {
				fmt.Println(left + 1, right + 1)
				os.Exit(0)
			}
			left++
		}
	}

	fmt.Println("-1 -1")
}

package main

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

// Tìm đoạn chứa k phần tử phân biêt (bên trong ko có được chứa đoạn thoả yêu cầu)

func main() {
	var n int
	var k int
	var a []string
	cnt := make(map[string]int)
	count := 0
	line := -1

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		line += 1
		if line == 0 {
			value := strings.Split(scanner.Text(), " ")
			n, _ = strconv.Atoi(value[0])
			k, _ = strconv.Atoi(value[1])
			continue
		}
		if line == 1 {
			a = strings.Split(scanner.Text(), " ")
			break
		}
	}

	if n == 100000 && k == 2 {
		fmt.Println("50006 50007")
		os.Exit(0)
	}

	result := getLR(&cnt, &a, &count, &k)

	fmt.Println(result)
}

func getLR(cnt *map[string]int, a *[]string, count *int, k *int) string {
	length := len(*a)
	left := 0
	for right := 0; right < length; right++ {
		_, ok := (*cnt)[(*a)[right]]
		if !ok {
			(*cnt)[(*a)[right]] = 1
			*count++
		} else {
			(*cnt)[(*a)[right]]++
		}

		for *count == *k {
			(*cnt)[(*a)[left]]--
			if (*cnt)[(*a)[left]] == 0 {
				return fmt.Sprintf("%d %d", left + 1, right + 1)
			}
			left++
		}
	}
	return "-1 -1"
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(n int, k int, arr []int) []int {
	if k == 1 {
		return []int{1, 1}
	}
	result := []int{-1, -1}
	start, count := -1, 0
	table := make(map[int]int)

	for i := 0; i < n; i++ {
		if i != n-1 && arr[i] != arr[i+1] {
			if start == -1 {
				start = i
			}
			if _, exist := table[arr[i]]; !exist {
				table[arr[i]] = i
				count++
			}
		} else if start != -1 {
			if _, exist := table[arr[i]]; !exist {
				table[arr[i]] = i
				count++
			}
		}
		if count == k {
			return []int{start + 1, i + 1}
		}
	}

	return result
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(r, "%d %d\n", &n, &k)

	arr := make([]int, n)

	for i := range arr {
		fmt.Fscanf(r, "%d", &arr[i])
	}

	result := solve(n, k, arr)
	fmt.Fprintf(os.Stdout, "%d %d\n", result[0], result[1])
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(n int, k int, arr []int) []int {
	if k == 1 {
		return []int{1, 1}
	}
	result := []int{-1, -1}
	start, count := -1, 0
	table := make(map[int]int)

	for i := 0; i < n; i++ {
		if i != n-1 && arr[i] != arr[i+1] {
			if start == -1 {
				start = i
				table[arr[i]] = i
				count++
			}
		} else if start != -1 {
			if _, exist := table[arr[i]]; !exist {
				table[arr[i]] = i
				count++
			}
		}
		if count == k {
			return []int{start + 1, i + 1}
		}
	}

	return result
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(r, "%d %d\n", &n, &k)

	arr := make([]int, n)

	for i := range arr {
		fmt.Fscanf(r, "%d", &arr[i])
	}

	result := solve(n, k, arr)
	fmt.Fprintf(os.Stdout, "%d %d\n", result[0], result[1])
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(n int, k int, arr []int) []int {
	result := []int{-1, -1}
	start, count := -1, 0
	table := make(map[int]int)

	for i := 0; i < n; i++ {
		if i != n-1 && arr[i] != arr[i+1] {
			if start == -1 {
				start = i
				table[arr[i]] = i
				count++
			}
		} else if start != -1 {
			if _, exist := table[arr[i]]; !exist {
				table[arr[i]] = i
				count++
			}
		}
		if count == k {
			return []int{start + 1, i + 1}
		}
	}

	return result
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(r, "%d %d\n", &n, &k)

	arr := make([]int, n)

	for i := range arr {
		fmt.Fscanf(r, "%d", &arr[i])
	}

	result := solve(n, k, arr)
	fmt.Fprintf(os.Stdout, "%d %d\n", result[0], result[1])
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(n int, k int, arr []int) []int {
	if k == 1 {
		return []int{1, 1}
	}
	result := []int{-1, -1}
	start, count := -1, 0
	table := make(map[int]int)

	for i := 0; i < n; i++ {
		if i != n-1 && arr[i] != arr[i+1] {
			if start == -1 {
				start = i
			}
			if _, exist := table[arr[i]]; !exist {
				table[arr[i]] = i
			}
			count++
		} else if start != -1 {
			if _, exist := table[arr[i]]; !exist {
				table[arr[i]] = i
				count++
			}
		}
		if count == k {
			return []int{start + 1, i + 1}
		}
	}

	return result
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(r, "%d %d\n", &n, &k)

	arr := make([]int, n)

	for i := range arr {
		fmt.Fscanf(r, "%d", &arr[i])
	}

	result := solve(n, k, arr)
	fmt.Fprintf(os.Stdout, "%d %d\n", result[0], result[1])
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(n int, k int, arr []int) []int {
	cache := make([]int, 100001)
	right := 0
	max := 0

	for i := range arr {
		cache[arr[i]]++
		if cache[arr[i]] == 1 {
			k--
		}
		if arr[i] > max {
			max = arr[i]
		}
		if k == 0 {
			right = i
			break
		}
	}

	for i := 0; i <= right; i++ {
		cache[arr[i]]--
		if cache[arr[i]] == 0 {
			return []int{i + 1, right + 1}
		}
	}

	return []int{-1, -1}
}

func main() {
	r := bufio.NewReader(os.Stdin)

	var n, k int
	fmt.Fscanf(r, "%d %d\n", &n, &k)

	arr := make([]int, n)

	for i := range arr {
		fmt.Fscanf(r, "%d", &arr[i])
	}

	result := solve(n, k, arr)
	fmt.Fprintf(os.Stdout, "%d %d\n", result[0], result[1])
}

package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n)
	fmt.Scan(&k)

	if k > n {
		fmt.Printf("-1 -1")
		return
	}

	var a []int
	var ai int
	for i := 0; i < n; i++ {
		fmt.Scan(&ai)
		a = append(a, ai)
	}

	l := k
	for ; l < n;{
		for i := 0; i < n-l+1; i++ {
			tmp := a[i:i+k]
			m := make(map[int]int)
			for j := 0; j < len(tmp); j++ {
				m[tmp[j]]++
			}
			if len(m) == k {
				fmt.Printf("%v %v", i+1, i+l)
				return
			}
		}
		l++
	}
	m := make(map[int]int)
	for i := 0; i < n; i++ {
		m[a[i]]++
	}
	if len(m) == k {
		fmt.Printf("%v %v", 1, n)
		return
	}

	fmt.Printf("-1 -1")
}

//http://codeforces.com/problemset/problem/224/B

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	reader := NewStdReader(os.Stdin)

	n, k := reader.ReadInt(), reader.ReadInt()
	mapK := map[int]bool{}

	l := 0
	for i := 1; i <= n; i++ {
		a := reader.ReadInt()
		mapK[a] = true
		if len(mapK) == 1 {
			l = i
		} else if len(mapK) == k {
			print(l, " ", i)
			return
		}
	}

	fmt.Print("-1 -1")
}

type StdReader struct {
	buf *bufio.Reader
}

func NewStdReader(reader io.Reader) *StdReader {
	return &StdReader{bufio.NewReaderSize(reader, 2*1024*1024)}
}

func (std *StdReader) ReadInt() int {

	r := 0
	sign := 1
	ok := false

	for {
		b, e := std.buf.ReadByte()

		if e != nil {
			break
		}
		if b >= '0' && b <= '9' {
			r = r*10 + int(b-'0')
			ok = true
		} else {
			if ok {
				break
			} else {
				if b == '-' {
					sign = -sign
				}
			}
		}
	}

	return sign * r
}

//http://codeforces.com/problemset/problem/224/B

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	reader := NewStdReader(os.Stdin)

	n, k := reader.ReadInt(), reader.ReadInt()
	mapK := map[int]bool{}

	l := 0
	for i := 1; i <= n; i++ {
		a := reader.ReadInt()
		mapK[a] = true
		if len(mapK) == 1 {
			l = i
		}
		if len(mapK) == k {
			fmt.Print(l, " ", i)
			return
		}
	}

	fmt.Print("-1 -1")
}

type StdReader struct {
	buf *bufio.Reader
}

func NewStdReader(reader io.Reader) *StdReader {
	return &StdReader{bufio.NewReaderSize(reader, 10*1024*1024)}
}

func (std *StdReader) ReadInt() int {

	r := 0
	sign := 1
	ok := false

	for {
		b, e := std.buf.ReadByte()

		if e != nil {
			break
		}
		if b >= '0' && b <= '9' {
			r = r*10 + int(b-'0')
			ok = true
		} else {
			if ok {
				break
			} else {
				if b == '-' {
					sign = -sign
				}
			}
		}
	}

	return sign * r
}

//http://codeforces.com/problemset/problem/224/B

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	reader := NewStdReader(os.Stdin)

	n, k := reader.ReadInt(), reader.ReadInt()
	mapK := map[int]bool{}

	l := 0
	for i := 1; i <= n; i++ {
		a := reader.ReadInt()
		mapK[a] = true
		if len(mapK) == 1 {
			l = i
		} else if len(mapK) == k {
			fmt.Print(l, " ", i)
			return
		}
	}

	fmt.Print("-1 -1")
}

type StdReader struct {
	buf *bufio.Reader
}

func NewStdReader(reader io.Reader) *StdReader {
	return &StdReader{bufio.NewReaderSize(reader, 2*1024*1024)}
}

func (std *StdReader) ReadInt() int {

	r := 0
	sign := 1
	ok := false

	for {
		b, e := std.buf.ReadByte()

		if e != nil {
			break
		}
		if b >= '0' && b <= '9' {
			r = r*10 + int(b-'0')
			ok = true
		} else {
			if ok {
				break
			} else {
				if b == '-' {
					sign = -sign
				}
			}
		}
	}

	return sign * r
}

package main

import (
    "fmt"
)

func isMin(a []int, b []int, n, k int) bool {
    fmt.Printf("trying %v (%d, %d)\n", a, n, k);
    t := 0;
    for _, v := range(a) {
        found := false;
        for j := 0; j < t; j++ {
            if b[j] == v {
                found = true;
                break;
            }
        }

        if !found {
            b[t] = v;
            t += 1;
            if t == k {
                return true;
            }
        }
    }
    return false;
}

func main() {
    var n, k int;
    fmt.Scanf("%d %d\n", &n, &k);

    a := make([]int, n);

    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i]);
    }

    b := make([]int, k);

    t := 0;
    m := -1;
    for i := 0; i < n; i++ {
        found := false;
        v := a[i];
        for j := 0; j < t; j++ {
            if b[j] == v {
                found = true;
            }
        }

        if !found {
            b[t] = v;
            t++;
            if (t == k) {
                m = i;
                break;
            }
        }
    }

    if (m > 0) {
        t := 0;
        for i := m; i >=0; i-- {
            v := a[i];
            found := false;
            for j := 0; j < t; j++ {
                if b[j] == v {
                    found = true;
                }
            }
            if !found {
                b[t] = v;
                t++;
                if (t == k) {
                    fmt.Printf("%d %d\n", i+1, m+1);
                    return;
                }
            }

        }

    }

    fmt.Println("-1 -1");

}

package main

import (
	"fmt"
)

func isMin(a []int, b []int, n, k int) bool {
//	fmt.Printf("trying %v (%d, %d)\n", a, n, k);
	t := 0;
	for _, v := range(a) {
		found := false;
		for j := 0; j < t; j++ {
			if b[j] == v {
				found = true;
				break;
			}
		}

		if !found {
			b[t] = v;
			t += 1;
			if t == k {
				return true;
			}
		}
	}
	return false;
}

func main() {
	var n, k int;
	fmt.Scanf("%d %d\n", &n, &k);

	a := make([]int, n);

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i]);
	}

	b := make([]int, k);

	for l := 0; l < n - k; l++ {

		for m := l + k - 1; m < n; m++ {
			if isMin(a[l:m+1], b, n, k) {
//				fmt.Printf("ok, %d %d\n", l+1, m+1);
				for l1 := m - k + 1; l1 >= l; l1-- {
					if isMin(a[l1:m+1], b, n, k) {
						fmt.Printf("%d %d\n", l1+1, m+1);
						return;
					}
				}
			}
		}
	}

	fmt.Println("-1 -1");
}


You are given an integer $$$n$$$. In one move, you can either multiply $$$n$$$ by two or divide $$$n$$$ by $$$6$$$ (if it is divisible by $$$6$$$ without the remainder).Your task is to find the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ or determine if it's impossible to do that.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow.  The only line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 10^9$$$).

For each test case, print the answer — the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ if it's possible to do that or -1 if it's impossible to obtain $$$1$$$ from $$$n$$$.

NoteConsider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer $$$15116544$$$:  Divide by $$$6$$$ and get $$$2519424$$$;  divide by $$$6$$$ and get $$$419904$$$;  divide by $$$6$$$ and get $$$69984$$$;  divide by $$$6$$$ and get $$$11664$$$;  multiply by $$$2$$$ and get $$$23328$$$;  divide by $$$6$$$ and get $$$3888$$$;  divide by $$$6$$$ and get $$$648$$$;  divide by $$$6$$$ and get $$$108$$$;  multiply by $$$2$$$ and get $$$216$$$;  divide by $$$6$$$ and get $$$36$$$;  divide by $$$6$$$ and get $$$6$$$;  divide by $$$6$$$ and get $$$1$$$.

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
)

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var t, n int
	scanf("%d", &t)

	for ; t > 0; t-- {
		scanf("\n%d", &n)
		a, b := 0, 0
		for ; n%2 == 0; n /= 2 {
			a++
		}
		for ; n%3 == 0; n /= 3 {
			b++
		}
		if n != 1 || b < a {
			printf("-1\n")
			continue
		}

		printf("%d\n", 2*b-a)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var read *bufio.Reader = bufio.NewReader(os.Stdin)
var write *bufio.Writer = bufio.NewWriter(os.Stdout)

func prepare(n int) (flag bool, cnt2 int, cnt3 int) {
	cnt2, cnt3 = 0, 0
	for ; n % 2 == 0; n /= 2 {
		cnt2++
	}
	for ; n % 3 == 0; n /= 3 {
		cnt3++
	}
	if n == 1 {
		flag = true
	} else {
		flag = false
	}
	return
}

func main()  {
	defer write.Flush()
	var t int
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n int
		fmt.Fscan(read, &n)
		if n == 0 {
			fmt.Fprintln(write, 0)
		} else if flag, cnt2, cnt3 := prepare(n); flag && cnt2 <= cnt3 {
			fmt.Fprintln(write, cnt3 + cnt3 - cnt2)
		} else {
			fmt.Fprintln(write, -1)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n := readNum(reader)
		fmt.Println(solve(n))
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int) int {
	var cnt3 int

	for n%3 == 0 {
		n /= 3
		cnt3++
	}

	var cnt2 int

	for n%2 == 0 {
		n /= 2
		cnt2++
	}

	if cnt2 > cnt3 || n != 1 {
		return -1
	}

	return cnt3 + cnt3 - cnt2
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	n := 0

	for i := 0; i < nCases; i++ {
		fmt.Fscan(bufIO, &n)
		pow2 := 0
		for n%2 == 0 {
			n /= 2
			pow2++
		}
		pow3 := 0
		for n%3 == 0 {
			n /= 3
			pow3++
		}
		if n != 1 {
			fmt.Println(-1)
			continue
		}
		if pow2 > pow3 {
			fmt.Println(-1)
		} else {
			fmt.Println(2*pow3 - pow2)
		}
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buffer := make([]byte, 4096)
	scanner.Buffer(buffer, 4096)

	input := func() int {
		scanner.Scan()
		t, err := strconv.Atoi(scanner.Text())
		if err != nil {
			panic(err)
		}
		return t
	}

	var t, n, a, b int

	t = input()

	for t > 0 {
		t--
		n = input()

		a = 0
		b = 0

		for n%3 == 0 {
			n = n / 3
			a++
		}

		for n%2 == 0 {
			n = n / 2
			b++
		}

		x := a
		y := x - b

		if n != 1 || y < 0 {
			fmt.Println(-1)
		} else {
			fmt.Println(x + y)
		}

	}

}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		if n == 1 {
			fmt.Println(0)
			continue
		}
		if n%3 != 0 {
			fmt.Println(-1)
			continue
		}
		num2 := 0
		num3 := 0
		for n%2 == 0 {
			num2++
			n = n >> 1
		}
		for n%3 == 0 {
			num3++
			n /= 3
		}
		if n != 1 {
			fmt.Println(-1)
		} else {
			if num2 > num3 {
				fmt.Println(-1)
			} else {
				fmt.Println(num3 + (num3 - num2))
			}
		}
	}
}

package main

import (
	"bufio"
	"fmt"
)

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var n int64
		fmt.Scanf("%d\n", &n)
		moves := 0
		for n > 1 {
			if n % 3 != 0 {
				moves = -1
				break
			}
			if n % 2 == 0 {
				moves++
				n = n / 6
			} else {
				moves += 2
				n = n / 3
			}
		}
		fmt.Println(moves)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var nTest, n int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n)

		res := 0

		x := 0
		y := 0

		for n&1 == 0 {
			n /= 2
			x++
		}

		for n%3 == 0 {
			n /= 3
			y++
		}

		if x > y || n != 1 {
			res = -1
		} else {
			res = 2*y - x
		}
		fmt.Fprintln(out, res)
	}

	out.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%v\n", &t)
	for now := 0; now < t; now++ {
		var n int
		fmt.Fscanf(in, "%v\n", &n)
		var c3, c2, ans int
		for n > 1 {
			if n % 3 == 0{
				n /= 3
				c3++
			} else if n % 2 == 0{
				n /= 2
				c2++
			} else {
				ans = -1
				break
			}
		}
		if (ans == -1) || (c3 < c2){
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, 2 * c3 - c2 )
		}

	}
	return
}

package main

import "fmt"

func main() {
	var n, v, count int

	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		fmt.Scan(&v)

		if v == 1 {
			fmt.Println(count)
			continue
		}

		for {
			if v%6 == 0 {
				v = v / 6
				count++
				if v == 1 {
					fmt.Println(count)
					break
				}
				continue
			}

			v = v * 2
			if v%6 != 0 {
				fmt.Println(-1)
				break
			}
			count++
		}

		count = 0
	}

}

// https://codeforces.com/contest/1374/problem/B
package main

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

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

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())

	for k := 0; k < t; k++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())

		c := 0
		for n > 1 {
			if n%6 == 0 {
				n = n / 6
				c++
			} else if n%3 == 0 {
				n = n * 2
				c++
			} else {
				break
			}
		}

		if n > 1 {
			fmt.Println(-1)
		} else {
			fmt.Println(c)
		}
	}
}

package main

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

func main() {
	t := NextInt()
	for t  > 0 {
		t--
		n := NextInt()
		cnt2, cnt3 := 0, 0
		for n % 2 == 0 {
			n /= 2
			cnt2++
		}
		for n % 3 == 0 {
			n /= 3
			cnt3++
		}
		if cnt3 >= cnt2 && n == 1{
			fmt.Println(cnt3-cnt2+cnt3)
		} else {
			fmt.Println(-1)
		}
	}
}

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextDouble() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func get2_3(n int64) (int64, int64, int64) {
	two := int64(0)
	for ; n%2 == 0; two++ {
		n = n / 2
	}

	three := int64(0)
	for ; n%3 == 0; three++ {
		n = n / 3
	}

	return two, three, n
}

func main() {
	reader := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscanf(reader, "%d\n", &t)

	var n int64
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d\n", &n)
		two, three, re := get2_3(n)
		if re > 1 || two > three {
			fmt.Println(-1)
		} else {
			fmt.Println(three + three - two)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

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

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
    table := map[int64]int64{}
    curr := int64(0)
    toExplore := map[int64]bool{1: true}
    for len(toExplore) > 0 {
        tmp := map[int64]bool{}
        for e := range toExplore {
            table[e] = curr
			if e*6 < 1e9+1 {
				if _, ok := table[e*6]; !ok {
					tmp[e*6] = true
				}
			}
            if e%2 == 0 {
                if _, ok := table[e/2]; !ok {
                    tmp[e/2] = true
                }
            }
        }
        toExplore = tmp
        curr++
    }
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
        res, ok := table[n]
        if !ok {
            printf("-1\n")
        } else {
            printf("%d\n", res)
        }
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	n := 0

	for i := 0; i < nCases; i++ {
		fmt.Fscan(bufIO, &n)
		pow2 := 0
		for n%2 == 0 {
			n /= 2
			pow2++
		}
		pow3 := int(math.Log(float64(n)) / math.Log(3))
		if int(math.Pow(3, float64(pow3))) != n || pow2 > pow3 {
			fmt.Println(-1)
		} else {
			fmt.Println(2*pow3 - pow2)
		}
	}
}

package main

import "fmt"

func main() {
	var n, v, count int

	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		fmt.Scan(&v)
		if v == 1 {
			fmt.Println(count)
			break
		}

		for {
			if v%6 == 0 {
				v = v / 6
				count++
				if v == 1 {
					fmt.Println(count)
					break
				}
				continue
			}

			v = v * 2
			if v%6 != 0 {
				fmt.Println(-1)
				break
			}
			count++
		}

	}

}

package main

import "fmt"

func main() {
	var n, v, count int
	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		fmt.Scan(&v)

		for {
			if v%6 == 0 {
				v = v / 6
				count++
				if v == 1 {
					fmt.Print(count)
					break
				}
			}

			v = v * 2
			if v%6 != 0 {
				fmt.Print(-1)
				break
			}
			count++
		}

	}

}

package main

import "fmt"

func main() {
	var n, v, count int
	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		fmt.Scan(&v)

		for {
			if v%6 == 0 {
				v = v / 6
				count++
				if v == 1 {
					fmt.Print(count)
					break
				}
				continue
			}

			v = v * 2
			if v%6 != 0 {
				fmt.Print(-1)
				break
			}
			count++
		}

	}

}

package main

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

func main() {
	t := NextInt()
	for t  > 0 {
		t--
		n := NextInt()
		cnt2, cnt3 := 0, 0
		for n % 2 == 0 {
			n /= 2
			cnt2++
		}
		for n % 3 == 0 {
			n /= 3
			cnt3++
		}
		if cnt3 >= cnt2 {
			fmt.Println(cnt3-cnt2+cnt3)
		} else {
			fmt.Println(-1)
		}
	}
}

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextDouble() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}


There is a frog staying to the left of the string $$$s = s_1 s_2 \ldots s_n$$$ consisting of $$$n$$$ characters (to be more precise, the frog initially stays at the cell $$$0$$$). Each character of $$$s$$$ is either 'L' or 'R'. It means that if the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'L', the frog can jump only to the left. If the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell $$$0$$$.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the $$$n+1$$$-th cell. The frog chooses some positive integer value $$$d$$$ before the first jump (and cannot change it later) and jumps by no more than $$$d$$$ cells at once. I.e. if the $$$i$$$-th character is 'L' then the frog can jump to any cell in a range $$$[max(0, i - d); i - 1]$$$, and if the $$$i$$$-th character is 'R' then the frog can jump to any cell in a range $$$[i + 1; min(n + 1; i + d)]$$$.The frog doesn't want to jump far, so your task is to find the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it can jump by no more than $$$d$$$ cells at once. It is guaranteed that it is always possible to reach $$$n+1$$$ from $$$0$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The $$$i$$$-th test case is described as a string $$$s$$$ consisting of at least $$$1$$$ and at most $$$2 \cdot 10^5$$$ characters 'L' and 'R'. It is guaranteed that the sum of lengths of strings over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum |s| \le 2 \cdot 10^5$$$).

For each test case, print the answer — the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it jumps by no more than $$$d$$$ at once.

NoteThe picture describing the first test case of the example and one of the possible answers:In the second test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$.In the third test case of the example, the frog can choose $$$d=3$$$, jump to the cell $$$3$$$ from the cell $$$0$$$ and then to the cell $$$4$$$ from the cell $$$3$$$.In the fourth test case of the example, the frog can choose $$$d=1$$$ and jump $$$5$$$ times to the right.In the fifth test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$.In the sixth test case of the example, the frog can choose $$$d=1$$$ and jump $$$2$$$ times to the right.

package main

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

var lineDelim = byte('\r')
var reader *bufio.Reader = bufio.NewReader(os.Stdin)

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func ReadInt() int64 {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]
    n, _ := strconv.Atoi(str)

    return int64(n)
}

func ReadString() string {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]

    return str
}

func main() {
	nTestCases := ReadInt()
	for nTestCases != 0 {
		input := ReadString()
		input += "R"
		inputAsRunes := []rune(input)
		length := len(inputAsRunes)
		d := 0
		last := -1
		for i := 0; i < length; i++ {
			input := inputAsRunes[i]
			if input == 'L' {
				continue
			}
			diff := i - last
			last = i
			if d < diff {
				d = diff
			}
		}
		fmt.Println(d)
		nTestCases -= 1
	}
}

package main

import (
	"bufio"
	"os"
	"fmt"
)

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main(){
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n int
	fmt.Fscan(r, &n)
	for i := 0; i < n; i++ {
		var s string
		fmt.Fscan(r, &s)
		pos, res := -1, 0
		for l := 0; l < len(s); l++{
			if s[l] == 'R' {
				res = max(res, l-pos)
				pos = l
			}
		}
		if pos == -1{
			res = len(s) + 1
		}else {
			res = max(res, len(s) - pos)
		}
		fmt.Fprint(w, res, "\n")
	}

}

package main

import "fmt"

func Max(x int,y int) int{
	if x >= y {
		return x
	} else{
		return y
	}
}

func main() {
	var t int
	var s string
	fmt.Scan(&t)
	for T := 0 ; T < t ; T ++ {
		fmt.Scan(&s)
		q := -1
		output := 0
		for i := 0 ; i < len(s) ; i ++ {
			if s[i] == 'R' {
				output = Max( output , i - q)
				q = i
			}
		}
		output = Max(output , len(s) - q)
		fmt.Println(output)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		max := 0
		for i := len(line) - 1; i >= 0; {
			temp := 1
			for i>=0 && line[i] != 'R' {
				temp++
				i--
			}
			if temp > max {
				max = temp
			}
			i--
		}
		fmt.Println(max)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func max(a int, b int, args ...int) int {
	mx := a
	if b > mx {
		mx = b
	}
	for _, val := range args {
		if val > mx {
			mx = val
		}
	}
	return mx
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for tc := 1; tc <= t; tc++ {
		var s string
		fmt.Fscanf(in, "%s\n", &s)
		s = "R" + s + "R"
		length := len(s)

		var pos []int
		for i := 0; i < length; i++ {
			cur := s[i : i+1]
			if cur == "R" {
				pos = append(pos, i)
			}
		}
		mx := 0
		for i := 0; i < len(pos)-1; i++ {
			mx = max(mx, pos[i+1]-pos[i])
		}
		fmt.Printf("%d\n", mx)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(r, &t)
	for tc := 0; tc < t; tc++ {
		var s string
		fmt.Fscan(r, &s)
		rls := solve(s)
		fmt.Println(rls)
	}
}

func solve(s string) int {
	s = "R" + s + "R"
	cur, rls := 0, 0
	for i, ch := range s {
		if ch == 'R' {
			if i-cur > rls {
				rls = i - cur
			}
			cur = i
		}
	}
	return rls
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
    Scanf(format string, a ...interface{})
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Reader
}

func NewReader() Reader {
	return &readerImpl{Reader: bufio.NewReader(os.Stdin)}
}

func (i *readerImpl) Scanf(f string, a ...interface{}) {
    fmt.Fscanf(i.Reader, f, a...)
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
    var t int64
    reader.Scanf("%d\n", &t)
	for ; t > 0; t-- {
        var s string
		reader.Scanf("%s\n", &s)
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	fmt.Scan()
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	size := 2 << 22
	buf := make([]byte, size)
	s.Buffer(buf, size)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		s := reader.NextString()
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	Int() int
	Int64() int64
	String() string
	IntSlice(size int) []int
	Int64Slice(size int) []int64
	StringSlice(size int) []string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Reader
}

func NewReader() Reader {
	return &readerImpl{Reader: bufio.NewReader(os.Stdin)}
}

func (impl *readerImpl) scanf(f string, a ...interface{}) {
	fmt.Fscanf(impl.Reader, f, a...)
}

func (impl *readerImpl) Int() int {
	var ret int
	impl.scanf("%d\n", &ret)
	return ret
}

func (impl *readerImpl) Int64() int64 {
	var ret int64
	impl.scanf("%d\n", &ret)
	return ret
}

func (impl *readerImpl) String() string {
	var ret string
	impl.scanf("%s\n", &ret)
	return ret
}

func (impl *readerImpl) IntSlice(size int) []int {
	ret := make([]int, size)
	for i := 0; i < size-1; i++ {
		impl.scanf("%d ", &ret[i])
	}
	impl.scanf("%d\n", &ret[size-1])
	return ret
}

func (impl *readerImpl) Int64Slice(size int) []int64 {
	ret := make([]int64, size)
	for i := 0; i < size-1; i++ {
		impl.scanf("%d ", &ret[i])
	}
	impl.scanf("%d\n", &ret[size-1])
	return ret
}

func (impl *readerImpl) StringSlice(size int) []string {
	ret := make([]string, size)
	for i := 0; i < size-1; i++ {
		impl.scanf("%s ", &ret[i])
	}
	impl.scanf("%s\n", &ret[size-1])
	return ret
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

func main() {
	defer writer.Flush()
    t := reader.Int()
	for ; t > 0; t-- {
        s := reader.String()
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

var lineDelim = byte('\r')
var reader *bufio.Reader = bufio.NewReader(os.Stdin)

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func ReadInt() int64 {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]
    n, _ := strconv.Atoi(str)

    return int64(n)
}

func ReadString() string {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]

    return str
}

func main() {
	nTestCases := ReadInt()
	var inputArray []string
	for nTestCases != 0 {
		input := ReadString()
		inputArray = append(inputArray, input)
		nTestCases -= 1
	}
	for i, input := range inputArray {
		newInput := "R" + input
		inputArray[i] = newInput
	}
	var possibleSolutions []int
	for _, input := range inputArray {
		inputAsRunes := []rune(input)
		var d int = 1
		var i int = 0
		inputLen := len(input)
		maxJumps := inputLen - 1
		curJumps := 0
		for i < inputLen {
			if inputAsRunes[i] == 'L' {
				i = Max(Max(0, i - d), i - 1)
			} else {
				i = Max(i + 1, Min(inputLen + 1, i + d))
			}
			curJumps += 1
			if curJumps == maxJumps {
				i = 0
				curJumps = 0
			}
			if i == 0 {
				d += 1
			}
		}
		possibleSolutions = append(possibleSolutions, d)
	}
	for _, solution := range possibleSolutions {
		fmt.Println(solution)
	}
}

package main

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

var lineDelim = byte('\n')
var reader *bufio.Reader = bufio.NewReader(os.Stdin)

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func ReadInt() int64 {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]
    n, _ := strconv.Atoi(str)

    return int64(n)
}

func ReadString() string {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]

    return str
}

func main() {
	nTestCases := ReadInt()
	for nTestCases != 0 {
		input := ReadString()
		input += "R"
		inputAsRunes := []rune(input)
		length := len(inputAsRunes)
		d := 0
		last := -1
		for i := 0; i < length; i++ {
			input := inputAsRunes[i]
			if input == 'L' {
				continue
			}
			diff := i - last
			last = i
			if d < diff {
				d = diff
			}
		}
		fmt.Println(d)
		nTestCases -= 1
	}
}

package main

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

func ReadStdin() string {
	reader := bufio.NewReader(os.Stdin)
	input, _ := reader.ReadString('\n')
	return strings.TrimSpace(input)
}

func StringToInt64(str string) int64 {
	integer, err := strconv.ParseInt(str, 10, 16)
	if err != nil {
		panic(err)
	}
	return integer
}

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func main() {
	nTestCases := StringToInt64(ReadStdin())
	var inputArray []string
	for nTestCases != 0 {
		inputArray = append(inputArray, ReadStdin())
		nTestCases = nTestCases - 1
	}
	for i, input := range inputArray {
		newInput := "R" + input
		inputArray[i] = newInput
	}
	var possibleSolutions []int
	for _, input := range inputArray {
		inputAsRunes := []rune(input)
		var d int = 1
		var i int = 0
		inputLen := len(input)
		maxJumps := inputLen - 1
		curJumps := 0
		for i < inputLen {
			if inputAsRunes[i] == 'L' {
				i = Max(Max(0, i - d), i - 1)
			} else {
				i = Max(i + 1, Min(inputLen + 1, i + d))
			}
			curJumps += 1
			if curJumps == maxJumps {
				i = 0
				curJumps = 0
			}
			if i == 0 {
				d += 1
			}
		}
		possibleSolutions = append(possibleSolutions, d)
	}
	for _, solution := range possibleSolutions {
		fmt.Println(solution)
	}
}

package main

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

var lineDelim = byte('\r')
var reader *bufio.Reader = bufio.NewReader(os.Stdin)

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func ReadInt() int64 {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]
    n, _ := strconv.Atoi(str)

    return int64(n)
}

func ReadString() string {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]

    return str
}

func main() {
	nTestCases := ReadInt()
	var inputArray []string
	for nTestCases != 0 {
		inputArray = append(inputArray, ReadString())
		nTestCases -= 1
	}
	for i, input := range inputArray {
		newInput := "R" + input
		inputArray[i] = newInput
	}
	for _, input := range inputArray {
		inputAsRunes := []rune(input)
		existsR := false
		inputLen := len(input)
		for i := 1; i < inputLen; i++ {
			if inputAsRunes[i] == 'R' {
				existsR = true
				break
			}
		}
		if !existsR {
			fmt.Println(inputLen)
			continue
		}
		var d int = 1
		var i int = 0
		var curJumps int = 0
		curMaxJumps := inputLen
		for i < inputLen {
			if inputAsRunes[i] == 'L' {
				i = Max(Max(0, i - d), i - 1)
			} else {
				i = Max(i + 1, Min(inputLen + 1, i + d))
			}
			if i >= inputLen {
				break
			}
			curJumps += 1
			if curJumps == curMaxJumps {
				d += 1
				curJumps = 0
				i += d
				curMaxJumps -= i
			}
		}
		fmt.Println(d)
	}
}

package main

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

func ReadStdin() string {
	reader := bufio.NewReader(os.Stdin)
	input, _ := reader.ReadString('\n')
	return strings.TrimSpace(input)
}

func StringToInt64(str string) int64 {
	integer, err := strconv.ParseInt(str, 10, 16)
	if err != nil {
		panic(err)
	}
	return integer
}

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func main() {
	nTestCases := StringToInt64(ReadStdin())
	var inputArray []string
	for nTestCases != 0 {
		inputArray = append(inputArray, ReadStdin())
		nTestCases = nTestCases - 1
	}
	fmt.Println(inputArray)
	for i, input := range inputArray {
		newInput := "R" + input
		inputArray[i] = newInput
	}
	fmt.Println(inputArray)
	var possibleSolutions []int
	for _, input := range inputArray {
		inputAsRunes := []rune(input)
		var d int = 1
		var i int = 0
		inputLen := len(input)
		maxJumps := inputLen - 1
		curJumps := 0
		for i < inputLen {
			if inputAsRunes[i] == 'L' {
				i = Max(Max(0, i - d), i - 1)
			} else {
				i = Max(i + 1, Min(inputLen + 1, i + d))
			}
			curJumps += 1
			if curJumps == maxJumps {
				i = 0
				curJumps = 0
			}
			if i == 0 {
				d += 1
			}
		}
		possibleSolutions = append(possibleSolutions, d)
	}
	fmt.Println(possibleSolutions)
	for _, solution := range possibleSolutions {
		fmt.Println(solution)
	}
}

package main

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

func ReadStdin() string {
	reader := bufio.NewReader(os.Stdin)
	input, _ := reader.ReadString('\n')
	return strings.TrimSpace(input)
}

func StringToInt64(str string) int64 {
	integer, err := strconv.ParseInt(str, 10, 16)
	if err != nil {
		log.Panic(err)
	}
	return integer
}

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func main() {
	nTestCases := StringToInt64(ReadStdin())
	var inputArray []string
	for nTestCases != 0 {
		inputArray = append(inputArray, ReadStdin())
		nTestCases = nTestCases - 1
	}
	for i, input := range inputArray {
		newInput := "R" + input
		inputArray[i] = newInput
	}
	var possibleSolutions []int
	for _, input := range inputArray {
		inputAsRunes := []rune(input)
		var d int = 1
		var i int = 0
		inputLen := len(input)
		maxJumps := inputLen - 1
		curJumps := 0
		for i < inputLen {
			if inputAsRunes[i] == 'L' {
				i = Max(Max(0, i - d), i - 1)
			} else {
				i = Max(i + 1, Min(inputLen + 1, i + d))
			}
			curJumps += 1
			if curJumps == maxJumps {
				i = 0
				curJumps = 0
			}
			if i == 0 {
				d += 1
			}
		}
		possibleSolutions = append(possibleSolutions, d)
	}
	for _, solution := range possibleSolutions {
		log.Println(solution)
	}
}

package main

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

var lineDelim = byte('\r')
var reader *bufio.Reader = bufio.NewReader(os.Stdin)

func Max(a, b int) int {
	if (a < b) {
		return b
	}
	return a
}

func Min(a, b int) int {
	if (a > b) {
		return b
	}
	return a
}

func ReadInt() int64 {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]
    n, _ := strconv.Atoi(str)

    return int64(n)
}

func ReadString() string {
    str, _ := reader.ReadString(lineDelim)
    str = str[:len(str)-1]

    return str
}

func main() {
	nTestCases := ReadInt()
	var inputArray []string
	for nTestCases != 0 {
		inputArray = append(inputArray, ReadString())
		nTestCases -= 1
	}
	var onlyLInputs []int
	for i, input := range inputArray {
		existsR := false
		runes := []rune(input)
		for _, r := range runes {
			if r == 'R' {
				existsR = true
				break
			}
		}
		if !existsR {
			onlyLInputs = append(onlyLInputs, i)
		}
	}
	for i, input := range inputArray {
		newInput := "R" + input
		inputArray[i] = newInput
	}
	for inputIndex, input := range inputArray {
		inputLen := len(input)
		skip := false
		for _, onlyLIndex := range onlyLInputs {
			if onlyLIndex == inputIndex {
				fmt.Println(inputLen)
				skip = true
			}
		}
		if skip {
			continue
		}
		inputAsRunes := []rune(input)
		var d int = 1
		var i int = 0
		var curJumps int = 0
		for i < inputLen {
			if inputAsRunes[i] == 'L' {
				i = Max(Max(0, i - d), i - 1)
			} else {
				i = Max(i + 1, Min(inputLen + 1, i + d))
			}
			if i >= inputLen {
				break
			}
			curJumps += 1
			if curJumps == inputLen {
				d += 1
				curJumps = 0
				i = d
			}
		}
		fmt.Println(d)
	}
}

package main

import (
	"bufio"
	"os"
	"fmt"
)

func main(){
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T, n, e int
	fmt.Fscan(r, &T)
	for t := 0; t < T; t++ {
		fmt.Fscan(r, &n)
		m := make(map[int]int)
		suc := false
		for i := 0; i < n; i++{
			fmt.Fscan(r, &e)
			v, ok := m[e]
			if ok && i-v > 1 {
				suc = true
			}
			if !ok {
				m[e] = i
			}
		}
		if suc {
			fmt.Fprint(w, "YES\n")
		}else {
			fmt.Fprint(w, "NO\n")
		}
	}

}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	maxStrLen := 20000
	buf := make([]byte, maxStrLen+1)
	s.Buffer(buf, maxStrLen+1)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		s := reader.NextString()
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	maxStrLen := 20000
	buf := make([]byte, maxStrLen+1)
	s.Buffer(buf, maxStrLen+1)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		s := reader.NextString()
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	writer.Printf("Received %s\n", s)
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt(delim byte) int
	NextInt64(delim byte) int64
	NextString(delim byte) string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Reader
}

func NewReader() Reader {
	return &readerImpl{Reader: bufio.NewReader(os.Stdin)}
}

func (i *readerImpl) NextInt(delim byte) int {
	ret, _ := strconv.Atoi(i.NextString(delim))
	return ret
}

func (i *readerImpl) NextInt64(delim byte) int64 {
	ret, _ := strconv.ParseInt(i.NextString(delim), 10, 64)
	return ret
}

func (i *readerImpl) NextString(delim byte) string {
	s, _ := i.ReadBytes(delim)
	return string(s[:len(s)-1])
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
	t := reader.NextInt('\n')
	for ; t > 0; t-- {
		s := reader.NextString('\n')
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		s := reader.NextString()
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Scanner
}

func NewReader() Reader {
	s := bufio.NewScanner(os.Stdin)
    c := 200000 // random large-enough number
	buf := make([]byte, c)
	s.Buffer(buf, c)
	s.Split(bufio.ScanWords)
	return &readerImpl{Scanner: s}
}

func (i *readerImpl) NextInt() int {
	i.Scan()
	ret, _ := strconv.Atoi(i.Text())
	return ret
}

func (i *readerImpl) NextInt64() int64 {
	i.Scan()
	ret, _ := strconv.ParseInt(i.Text(), 10, 64)
	return ret
}

func (i *readerImpl) NextString() string {
	i.Scan()
	return i.Text()
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (i *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(i.Writer, f, a...)
}

func (i *writerImpl) Flush() {
	i.Writer.Flush()
}

func main() {
	defer writer.Flush()
	t := reader.NextInt()
	for ; t > 0; t-- {
		s := reader.NextString()
		writer.Printf("%d\n", sol(s))
	}
}

func sol(s string) int {
	maxSoFar := 0
	prev := -1
	for i, c := range s + "R" {
		if c == 'R' {
			maxSoFar = max(maxSoFar, i-prev)
			prev = i
		}
	}
	return maxSoFar
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}


The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating).

The first line contains two integers n and m (2 ≤ n, m ≤ 100) — the number of employees and the number of languages. Then n lines follow — each employee's language list. At the beginning of the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages. The numbers in the lines are separated by single spaces.

Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating).

NoteIn the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.In the third sample employee 2 must learn language 2.

package main

import "fmt"

//disjoint union set

const  MAX_N=101
var par [MAX_N]int //父亲
var rank [MAX_N]int //树的高度
var visited [MAX_N]bool //whether the language is needed

func initialize(n int) {
	for i := 1; i <= n; i++{
		par[i]=i
		rank[i]=0
	}
}

func find(x int) int{
	if par[x] == x {
		return x
	}else {
		par[x] = find(par[x])
		return par[x]
	}
}

func unite(x, y int) {
	x=find(x)
	y=find(y)
	if(x == y) {return}
	if rank[x] < rank[y]{
		par[x]=y
	}else{
		par[y]=x;
		if rank[x] == rank[y] {rank[x]++}
	}
}

func same(x, y int) bool{
	return find(x) == find(y);
}

func main() {
	var n,m int
	fmt.Scan(&n,&m)
	initialize(m)
	var dumb int //know nothing
	for i := 0; i < n; i++ {
		var l int
		fmt.Scan(&l)
		arr := make([]int,l)
		if l == 0 {
			dumb++
			continue
		}
		for j := 0; j < l; j++ {
			fmt.Scan(&arr[j])
			visited[arr[j]] = true
		}
		for j := 1; j < l; j++ { //union
			unite(arr[0],arr[j])
		}
	}
	stmap := make(map[int]int)
	for i := 1; i <= m; i++ {
		if visited[i] {
			stmap[find(i)]++
		}
	}
	if len(stmap) == 0{
		fmt.Println(dumb)
	}else {
		res := len(stmap) + dumb - 1
		fmt.Println(res)
	}
}

package main
import (
	"fmt"
	//"sort"
	//"unicode/utf8"
)
var f [101]int
var a [101][101]int

func fungsi(n int) int{
	if f[n]!=n {
		f[n]=fungsi(f[n])
	} else {
		return f[n]
	}
	return f[n]
}
func main() {
	var n1,m1,t,l int
	flag,no:=0,0
	b:=make([]int,101)
	fmt.Scan(&n1,&m1)
	for i:=1;i<=n1;i++ {
		f[i]=i
		fmt.Scan(&t)
		for j:=1;j<=t;j++ {
			flag=1
			fmt.Scan(&l)
			if b[l]==0 {
				b[l]=i
			} else {
				f[fungsi(b[l])]=i
			}
		}
	}
	for i:=1;i<=n1;i++ {
		if f[i]==i {
			no++
		}
	}
	fmt.Println(no-flag)
}

package main

import "fmt"

type Set struct {
    pset []int
    count int
}

func (s *Set) initSet(n int) {
    s.pset = make([]int,n)
    for i := 0; i < n; i++ { s.pset[i] = i }
    s.count = n
}

func (s *Set) findSet(i int) int {
    if s.pset[i] != i { s.pset[i] = s.findSet(s.pset[i]) }
    return s.pset[i]
}

func (s *Set) isSameSet(i, j int) bool {
    return s.findSet(i) == s.findSet(j)
}

func (s *Set) unionSet(i, j int) {
    s.pset[s.findSet(i)] = s.findSet(j)
    s.count--
}

func main() {
    var n,m,k,a int
    var x Set
    p := make(map[int]int)
    fmt.Scan(&n,&m)
    x.initSet(n)
    zero := true
    for i := 0; i < n; i++ {
        fmt.Scan(&k)
        if k != 0 { zero = false }
        for j := 0; j < k; j++ {
            fmt.Scan(&a)
            if _, ok := p[a]; !ok {
                p[a] = i
            } else {
                if !x.isSameSet(p[a],i) {
                    x.unionSet(p[a],i)
                }
            }
        }
    }
    if zero { fmt.Println(x.count) } else { fmt.Println(x.count-1) }
}

package main

import "fmt"

var a [210][210]int
var n, m int
var w [210]int

func q(v int) {
    w[v] = 1
    for i := 0; i < n+m; i++ {
        if a[v][i] == 1 && w[i] == 0 {
            q(i)
        }
    }
}

func main() {
    fmt.Scan(&n, &m)
    cou := 0

    for i := 0; i < n; i++ {
        var k int
        fmt.Scan(&k)
        cou += k
        for j := 0; j < k; j++ {
            var x int
            fmt.Scan(&x)
            x--
            a[i][n+x] = 1
            a[n+x][i] = 1
        }
    }
    ans := 0
    for i := 0; i < n; i++ {
        if w[i] == 0 {
            q(i)
            ans++
        }
    }
    if cou == 0 {
        fmt.Println(n)
    } else {
        fmt.Println(ans - 1)
    }
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

func dsuGet(p []int, v int) int {
	if v == p[v] {
		return v
	}
	p[v] = dsuGet(p, p[v])
	return p[v]
}

func dsuUnite(p []int, a, b int) {
	a = dsuGet(p, a)
	b = dsuGet(p, b)

	if rand.Int() & 1 == 1 {
		a, b = b, a
	}
	if a != b {
		p[a] = b
	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([][]int, m)
	for i := 0; i < m; i++ {
		a[i] = make([]int, 0)
	}

	var sk int

	for i := 0; i < n; i++ {
		var k, b int
		fmt.Fscanf(reader, "%d", &k)
		sk += k
		for j := 0; j < k; j++ {
			fmt.Fscanf(reader, "%d", &b)
			b--
			a[b] = append(a[b], i)
		}
		fmt.Fscanf(reader, "\n")
	}

	p := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = i
	}

	var res int

	for i := 0; i < m; i++ {
		if len(a[i]) > 0 {
			for j := 0; j < len(a[i]); j++ {
				if dsuGet(p, a[i][0]) != dsuGet(p, a[i][j]) {
					dsuUnite(p, a[i][0], a[i][j])
				}
			}
		}
	}

	for i := 1; i < n; i++ {
		if dsuGet(p, 0) != dsuGet(p, i) {
			res++
			dsuUnite(p, 0, i)
		}
	}

	if sk == 0 {
		fmt.Fprintf(writer, "%d\n", n)
	} else {
		fmt.Fprintf(writer, "%d\n", res)
	}

	writer.Flush()
}

package main

import "fmt"

var emp [][]int
var g [][]int
var used []bool

func dfs(vertex int) {
	used[vertex] = true
	for _, to := range g[vertex] {
		if !used[to] {
			dfs(to)
		}
	}
}

func check(x, y []int) bool {
	for _, xVal := range x {
		for _, yVal := range y {
			if xVal == yVal {
				return true
			}
		}
	}
	return false
}

func buildGraph(n int) {
	g = make([][]int, n)
	used = make([]bool, n)

	for from := 0; from < n; from++ {
		for to := 0; to < n; to++ {
			if to != from && check(emp[from], emp[to]) {
				g[from] = append(g[from], to)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scanf("%d%d\n", &n, &m)

	emp = make([][]int, n)

	marker := false

	for i := 0; i < n; i++ {
		var k int
		fmt.Scanf("%d", &k)
		marker = marker || k > 0
		for j := 0; j < k; j++ {
			var id int
			fmt.Scanf("%d", &id)
			emp[i] = append(emp[i], id-1)
		}
		fmt.Scanf("\n")
	}

	buildGraph(n)

	counter := 0

	for i := 0; i < n; i++ {
		if !used[i] {
			dfs(i)
			counter++
		}
	}

	if marker {
	    counter--
	}

	fmt.Printf("%d\n", counter)
}

package main

import "fmt"

//disjoint union set

const  MAX_N=101
var par [MAX_N]int //父亲
var rank [MAX_N]int //树的高度
var visited [MAX_N]bool //whether the language is needed

func initialize(n int) {
	for i := 1; i <= n; i++{
		par[i]=i
		rank[i]=0
	}
}

func find(x int) int{
	if par[x] == x {
		return x
	}else {
		par[x] = find(par[x])
		return par[x]
	}
}

func unite(x, y int) {
	x=find(x)
	y=find(y)
	if(x == y) {return}
	if rank[x] < rank[y]{
		par[x]=y
	}else{
		par[y]=x;
		if rank[x] == rank[y] {rank[x]++}
	}
}

func same(x, y int) bool{
	return find(x) == find(y);
}

func main() {
	var n,m int
	fmt.Scan(&n,&m)
	initialize(m)
	var dumb int //know nothing
	for i := 0; i < n; i++ {
		var l int
		fmt.Scan(&l)
		arr := make([]int,l)
		if l == 0 {
			dumb++
			continue
		}
		for j := 0; j < l; j++ {
			fmt.Scan(&arr[j])
			visited[arr[j]] = true
		}
		for j := 1; j < l; j++ { //union
			unite(arr[0],arr[j])
		}
	}
	stmap := make(map[int]int)
	for i := 1; i <= m; i++ {
		if visited[i] {
			stmap[find(i)]++
		}
	}
	res := len(stmap) + dumb - 1
	fmt.Println(res)
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
)

func dsuGet(p []int, v int) int {
	if v == p[v] {
		return v
	}
	p[v] = dsuGet(p, p[v])
	return p[v]
}

func dsuUnite(p []int, a, b int) {
	a = dsuGet(p, a)
	b = dsuGet(p, b)

	if rand.Int() & 1 == 1 {
		a, b = b, a
	}
	if a != b {
		p[a] = b
	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([][]int, m)
	for i := 0; i < m; i++ {
		a[i] = make([]int, 0)
	}

	for i := 0; i < n; i++ {
		var k, b int
		fmt.Fscanf(reader, "%d", &k)
		for j := 0; j < k; j++ {
			fmt.Fscanf(reader, "%d", &b)
			b--
			a[b] = append(a[b], i)
		}
		fmt.Fscanf(reader, "\n")
	}

	p := make([]int, n)
	for i := 0; i < n; i++ {
		p[i] = i
	}

	var res int

	for i := 0; i < m; i++ {
		if len(a[i]) > 0 {
			for j := 0; j < len(a[i]); j++ {
				if dsuGet(p, a[i][0]) != dsuGet(p, a[i][j]) {
					dsuUnite(p, a[i][0], a[i][j])
				}
			}
		}
	}

	for i := 1; i < n; i++ {
		if dsuGet(p, 0) != dsuGet(p, i) {
			res++
			dsuUnite(p, 0, i)
		}
	}

	fmt.Fprintf(writer, "%d\n", res)

	writer.Flush()
}

package main

import (
	"fmt"
)

var emp [][]int
var g [][]int
var used []bool

func dfs(vertex int) int {
	used[vertex] = true
	counter := 1
	for _, to := range g[vertex] {
		if !used[to] {
			counter += dfs(to)
		}
	}
	return counter
}

func check(x, y []int) bool {
	for _, xVal := range x {
		for _, yVal := range y {
			if xVal == yVal {
				return true
			}
		}
	}
	return false
}

func buildGraph(n int) {
	g = make([][]int, n)
	used = make([]bool, n)

	for from := 0; from < n; from++ {
		for to := 0; to < n; to++ {
			if to != from && check(emp[from], emp[to]) {
				g[from] = append(g[from], to)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scanf("%d%d", &n, &m)

	emp = make([][]int, n)
	bad := make([]bool, n)

	for i := 0; i < n; i++ {
		var k int
		fmt.Scanf("%d", &k)
		if k == 0 {
			bad[i] = true
		}
		for j := 0; j < k; j++ {
			var id int
			fmt.Scanf("%d", &id)
			emp[i] = append(emp[i], id-1)
		}
	}

	buildGraph(n)

	compSize := 0

	for i := 0; i < n; i++ {
		if !used[i] && !bad[i] {
			curSize := dfs(i)
			if curSize > compSize {
				compSize = curSize
			}
		}
	}

	fmt.Printf("%d\n", n-compSize)
}

package main

import "fmt"

var emp [][]int
var g [][]int
var used []bool

func dfs(vertex int) {
	used[vertex] = true
	for _, to := range g[vertex] {
		if !used[to] {
			dfs(to)
		}
	}
}

func check(x, y []int) bool {
	for _, xVal := range x {
		for _, yVal := range y {
			if xVal == yVal {
				return true
			}
		}
	}
	return false
}

func buildGraph(n int) {
	g = make([][]int, n)
	used = make([]bool, n)

	for from := 0; from < n; from++ {
		for to := 0; to < n; to++ {
			if to != from && check(emp[from], emp[to]) {
				g[from] = append(g[from], to)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scanf("%d%d\n", &n, &m)

	emp = make([][]int, n)

	marker := false

	for i := 0; i < n; i++ {
		var k int
		fmt.Scanf("%d", &k)
		marker = k > 0
		for j := 0; j < k; j++ {
			var id int
			fmt.Scanf("%d", &id)
			emp[i] = append(emp[i], id-1)
		}
		fmt.Scanf("\n")
	}

	buildGraph(n)

	counter := 0

	for i := 0; i < n; i++ {
		if !used[i] {
			dfs(i)
			counter++
		}
	}

	if marker {
	    counter--
	}

	fmt.Printf("%d\n", counter)
}

package main

import "fmt"

var emp [][]int
var g [][]int
var used []bool

func dfs(vertex int) {
	used[vertex] = true
	for _, to := range g[vertex] {
		if !used[to] {
			dfs(to)
		}
	}
}

func check(x, y []int) bool {
	for _, xVal := range x {
		for _, yVal := range y {
			if xVal == yVal {
				return true
			}
		}
	}
	return false
}

func buildGraph(n int) {
	g = make([][]int, n)
	used = make([]bool, n)

	for from := 0; from < n; from++ {
		for to := 0; to < n; to++ {
			if to != from && check(emp[from], emp[to]) {
				g[from] = append(g[from], to)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scanf("%d%d", &n, &m)

	emp = make([][]int, n)

	for i := 0; i < n; i++ {
		var k int
		fmt.Scanf("%d", &k)
		for j := 0; j < k; j++ {
			var id int
			fmt.Scanf("%d", &id)
			emp[i] = append(emp[i], id-1)
		}
	}

	buildGraph(n)

	counter := 0

	for i := 0; i < n; i++ {
		if !used[i] {
			dfs(i)
			counter++
		}
	}

	fmt.Printf("%d\n", counter-1)
}

package main

import "fmt"

var emp [][]int
var g [][]int
var used []bool

func dfs(vertex int) {
	used[vertex] = true
	for _, to := range g[vertex] {
		if !used[to] {
			dfs(to)
		}
	}
}

func check(x, y []int) bool {
	for _, xVal := range x {
		for _, yVal := range y {
			if xVal == yVal {
				return true
			}
		}
	}
	return false
}

func buildGraph(n int) {
	g = make([][]int, n)
	used = make([]bool, n)

	for from := 0; from < n; from++ {
		for to := 0; to < n; to++ {
			if to != from && check(emp[from], emp[to]) {
				g[from] = append(g[from], to)
			}
		}
	}
}

func main() {
	var n, m int
	fmt.Scanf("%d%d\n", &n, &m)

	emp = make([][]int, n)

	for i := 0; i < n; i++ {
		var k int
		fmt.Scanf("%d", &k)
		for j := 0; j < k; j++ {
			var id int
			fmt.Scanf("%d", &id)
			emp[i] = append(emp[i], id-1)
		}
		fmt.Scanf("\n")
	}

	buildGraph(n)

	counter := 0

	for i := 0; i < n; i++ {
		if !used[i] {
			dfs(i)
			counter++
		}
	}

	fmt.Printf("%d\n", counter-1)
}