package main

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

func getNumFromOneLine(ir *bufio.Reader) int{
	stream, _ := ir.ReadString('\n')
	// Trims the stream and then splits
	trimmedStream := strings.TrimSpace(stream)
	num, _ := strconv.Atoi(trimmedStream)
	return num
}

func getNumsFromOneLine(ir *bufio.Reader) []int{
	stream, _ := ir.ReadString('\n')
	// Trims the stream and then splits
	trimmedStream := strings.TrimSpace(stream)
	splitArr := strings.Split(trimmedStream, " ")
	// convert strings to integers and store them in a slice
	nums := make([]int, len(splitArr))
	for index,val := range splitArr{
		nums[index], _ = strconv.Atoi(val)
	}
	return nums
}

func main() {
	ir := bufio.NewReader(os.Stdin)
	n := getNumFromOneLine(ir)
	nums := getNumsFromOneLine(ir)
	//fmt.Println(nums)
	lookup := make(map[int][]int)
	for i, num := range nums {
		lookup[num] = append(lookup[num], i)
	}
	// res最终可能会很大，所以需要用int64
	var res int64
	// 两个人都必须要先到达1, 所以要加上从0到1的距离
	res = int64(lookup[1][0] + lookup[1][1])
	// 每一轮两个人都要从i到i+1，无论这轮他们怎么走，之后的所有轮都是两个人从i+1出发，所以我们只要取得此轮最优即可
	for i := 1; i < n; i += 1 {
		// 一个人从第一个i到第一个i+1, 另一个人从第二个i到第二个i+1
		path1 := math.Abs(float64(lookup[i][0] - lookup[i+1][0])) + math.Abs(float64(lookup[i][1] - lookup[i+1][1]))
		// 一个人从第一个i到第二个i+1, 另一个人从第二个i到第一个i+1
		path2 := math.Abs(float64(lookup[i][0] - lookup[i+1][1])) + math.Abs(float64(lookup[i][1] - lookup[i+1][0]))
		// 加上这一轮的最优距离
		res += int64(math.Min(path1, path2))
	}
	fmt.Println(res)
}

package main

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

func cleanString(stream string, seperator string) []int{
	// Trims the stream and then splits
	trimmedStream := strings.TrimSpace(stream)
	splitArr := strings.Split(trimmedStream, seperator)
	// convert strings to integers and store them in a slice
	nums := make([]int, len(splitArr))
	for index,val := range splitArr{
		nums[index], _ = strconv.Atoi(val)
	}
	return nums
}

func main() {
	ir := bufio.NewReader(os.Stdin)
	input, _ := ir.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input))
	var nums []int
	lineInput, _ := ir.ReadString('\n')
	nums = cleanString(lineInput, " ")
	//fmt.Println(nums)
	lookup := make(map[int][]int)
	for i, num := range nums {
		lookup[num] = append(lookup[num], i)
	}

	var res int64
	// 两个人都必须要先到达1, res最终可能会很大，所以需要用int64
	res = int64(lookup[1][0] + lookup[1][1])
	// 每一轮两个人都要从i到i+1，无论这轮他们怎么走，之后的所有轮都是两个人从i+1出发，所以我们只要取得此轮最优即可
	for i := 1; i < n; i += 1 {
		// 一个人从第一个i到第一个i+1, 另一个人从第二个i到第二个i+1
		path1 := math.Abs(float64(lookup[i][0] - lookup[i+1][0])) + math.Abs(float64(lookup[i][1] - lookup[i+1][1]))
		// 一个人从第一个i到第二个i+1, 另一个人从第二个i到第一个i+1
		path2 := math.Abs(float64(lookup[i][0] - lookup[i+1][1])) + math.Abs(float64(lookup[i][1] - lookup[i+1][0]))
		// 加上这一轮的最优
		res += int64(math.Min(path1, path2))
	}
	fmt.Println(res)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	a := make([]int, (n+1)*2)
	f := make([][]int, n+1)
	for i := 1; i <= n; i++ {
		f[i] = make([]int, 0)
	}
	for i := 0; i < n*2; i++ {
		fmt.Fscanf(in, "%d", &a[i])
		f[a[i]] = append(f[a[i]], i)
	}
	fmt.Fscanf(in, "\n")
	ans := int64(0)
	ans += int64(f[1][0] + f[1][1])
	for i := 2; i <= n; i++ {
		//fmt.Println(i, f[i])
		ans += int64(min(abs(f[i][0]-f[i-1][0])+abs(f[i][1]-f[i-1][1]), abs(f[i][0]-f[i-1][1])+abs(f[i][1]-f[i-1][0])))
	}

	fmt.Fprintf(out, "%d\n", ans)
	return
}

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

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

package main

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

func abs(val int) int {
	if val >= 0 {
		return val
	} else {
		return -val
	}
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)

	a := make([]int, 2 * n)
	pos := make([][]int, n)
	dp := make([][]int64, n)

	for i := range a {
		fmt.Fscanf(buf, "%d", &a[i])
		a[i] -= 1
		pos[a[i]] = append(pos[a[i]], i)
		dp[a[i]] = append(dp[a[i]], 0)
	}

	ans := int64(pos[0][0] + pos[0][1])

	for i := 1; i < n; i++ {
		temp := abs(pos[i][0] - pos[i - 1][0]) + abs(pos[i][1] - pos[i - 1][1])
		temp2 := abs(pos[i][0] - pos[i - 1][1]) + abs(pos[i][1] - pos[i - 1][0])
		if temp < temp2 {
			ans += int64(temp)
		} else {
			ans += int64(temp2)
		}
	}

	fmt.Println(ans)
}

package main

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

func cleanString(stream string, seperator string) []int{
	// Trims the stream and then splits
	trimmedStream := strings.TrimSpace(stream)
	splitArr := strings.Split(trimmedStream, seperator)
	// convert strings to integers and store them in a slice
	nums := make([]int, len(splitArr))
	for index,val := range splitArr{
		nums[index], _ = strconv.Atoi(val)
	}
	return nums
}

func main() {
	ir := bufio.NewReader(os.Stdin)
	input, _ := ir.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input))
	var nums []int
	lineInput, _ := ir.ReadString('\n')
	nums = cleanString(lineInput, " ")
	//fmt.Println(nums)
	lookup := make(map[int][]int)
	for i, num := range nums {
		lookup[num] = append(lookup[num], i)
	}
	lookup[0] = []int{0, 0}
	i := 0
	// 两个人都必须要先到达1
	//res := lookup[1][0] + lookup[1][1]
	var res int64
	// 每一轮两个人都要从i到i+1，无论这轮他们怎么走，之后的所有轮都是两个人从i+1出发，所以我们只要取得此轮最优即可
	for i < n {
		// 一个人从第一个i到第一个i+1, 另一个人从第二个i到第二个i+1
		path1 := math.Abs(float64(lookup[i][0] - lookup[i+1][0])) + math.Abs(float64(lookup[i][1] - lookup[i+1][1]))
		// 一个人从第一个i到第二个i+1, 另一个人从第二个i到第一个i+1
		path2 := math.Abs(float64(lookup[i][0] - lookup[i+1][1])) + math.Abs(float64(lookup[i][1] - lookup[i+1][0]))
		// 加上这一轮的最优
		res += int64(math.Min(path1, path2))
		//print("res", res)
		i += 1
	}
	fmt.Println(res)
}

package main

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

func abs(val int) int {
	if val >= 0 {
		return val
	} else {
		return -val
	}
}

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(buf, "%d\n", &n)

	a := make([]int, 2 * n)
	pos := make([][]int, n)
	dp := make([][]int64, n)

	for i := range a {
		fmt.Fscanf(buf, "%d", &a[i])
		a[i] -= 1
		pos[a[i]] = append(pos[a[i]], i)
		dp[a[i]] = append(dp[a[i]], 0)
	}

	ans := pos[0][0] + pos[0][1]

	for i := 1; i < n; i++ {
		temp := abs(pos[i][0] - pos[i - 1][0]) + abs(pos[i][1] - pos[i - 1][1])
		temp2 := abs(pos[i][0] - pos[i - 1][1]) + abs(pos[i][1] - pos[i - 1][0])
		if temp < temp2 {
			ans += temp
		} else {
			ans += temp2
		}
	}

	fmt.Println(ans)
}

package main

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

func cleanString(stream string, seperator string) []int{
	// Trims the stream and then splits
	trimmedStream := strings.TrimSpace(stream)
	splitArr := strings.Split(trimmedStream, seperator)
	// convert strings to integers and store them in a slice
	nums := make([]int, len(splitArr))
	for index,val := range splitArr{
		nums[index], _ = strconv.Atoi(val)
	}
	return nums
}

func main() {
	ir := bufio.NewReader(os.Stdin)
	input, _ := ir.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input))
	var nums []int
	lineInput, _ := ir.ReadString('\n')
	nums = cleanString(lineInput, " ")
	//fmt.Println(nums)
	lookup := make(map[int][]int)
	for i, num := range nums {
		lookup[num] = append(lookup[num], i)
	}
	i := 1
	// 两个人都必须要先到达1
	res := lookup[1][0] + lookup[1][1]
	// 每一轮两个人都要从i到i+1，无论这轮他们怎么走，之后的所有轮都是两个人从i+1出发，所以我们只要取得此轮最优即可
	for i < n {
		// 一个人从第一个i到第一个i+1, 另一个人从第二个i到第二个i+1
		path1 := math.Abs(float64(lookup[i][0] - lookup[i+1][0])) + math.Abs(float64(lookup[i][1] - lookup[i+1][1]))
		// 一个人从第一个i到第二个i+1, 另一个人从第二个i到第一个i+1
		path2 := math.Abs(float64(lookup[i][0] - lookup[i+1][1])) + math.Abs(float64(lookup[i][1] - lookup[i+1][0]))
		// 加上这一轮的最优
		res += int(math.Min(path1, path2))
		//print("res", res)
		i += 1
	}
	fmt.Println(res)

}

package main

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

func cleanString(stream string, seperator string) []int{
	// Trims the stream and then splits
	trimmedStream := strings.TrimSpace(stream)
	splitArr := strings.Split(trimmedStream, seperator)
	// convert strings to integers and store them in a slice
	nums := make([]int, len(splitArr))
	for index,val := range splitArr{
		nums[index], _ = strconv.Atoi(val)
	}
	return nums
}

func main() {
	ir := bufio.NewReader(os.Stdin)
	input, _ := ir.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input))
	var nums []int
	lineInput, _ := ir.ReadString('\n')
	nums = cleanString(lineInput, " ")
	//fmt.Println(nums)
	lookup := make(map[int][]int)
	for i, num := range nums {
		lookup[num] = append(lookup[num], i)
	}

	var res int64
	// 两个人都必须要先到达1, res最终可能会很大，所以需要用int64
	res = int64(lookup[1][0] + lookup[1][1])
	// 每一轮两个人都要从i到i+1，无论这轮他们怎么走，之后的所有轮都是两个人从i+1出发，所以我们只要取得此轮最优即可
	for i := 1; i < n; i += 1 {
		// 一个人从第一个i到第一个i+1, 另一个人从第二个i到第二个i+1
		path1 := math.Abs(float64(lookup[i][0] - lookup[i+1][0])) + math.Abs(float64(lookup[i][1] - lookup[i+1][1]))
		// 一个人从第一个i到第二个i+1, 另一个人从第二个i到第一个i+1
		path2 := math.Abs(float64(lookup[i][0] - lookup[i+1][1])) + math.Abs(float64(lookup[i][1] - lookup[i+1][0]))
		// 加上这一轮的最优
		res += int64(math.Min(path1, path2))
		//print("res", res)
		i += 1
	}
	fmt.Println(res)
}

package main

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

func cleanString(stream string, seperator string) []int{
	// Trims the stream and then splits
	trimmedStream := strings.TrimSpace(stream)
	splitArr := strings.Split(trimmedStream, seperator)
	// convert strings to integers and store them in a slice
	nums := make([]int, len(splitArr))
	for index,val := range splitArr{
		nums[index], _ = strconv.Atoi(val)
	}
	return nums
}

func main() {
	ir := bufio.NewReader(os.Stdin)
	input, _ := ir.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input))
	var nums []int
	lineInput, _ := ir.ReadString('\n')
	nums = cleanString(lineInput, " ")
	//fmt.Println(nums)
	lookup := make(map[int][]int)
	for i, num := range nums {
		lookup[num] = append(lookup[num], i)
	}
	lookup[0] = []int{0, 0}
	i := 0
	// 两个人都必须要先到达1
	//res := lookup[1][0] + lookup[1][1]
	res := 0
	// 每一轮两个人都要从i到i+1，无论这轮他们怎么走，之后的所有轮都是两个人从i+1出发，所以我们只要取得此轮最优即可
	for i < n {
		// 一个人从第一个i到第一个i+1, 另一个人从第二个i到第二个i+1
		path1 := math.Abs(float64(lookup[i][0] - lookup[i+1][0])) + math.Abs(float64(lookup[i][1] - lookup[i+1][1]))
		// 一个人从第一个i到第二个i+1, 另一个人从第二个i到第一个i+1
		path2 := math.Abs(float64(lookup[i][0] - lookup[i+1][1])) + math.Abs(float64(lookup[i][1] - lookup[i+1][0]))
		// 加上这一轮的最优
		res += int(math.Min(path1, path2))
		//print("res", res)
		i += 1
	}
	fmt.Println(res)

}


package main

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

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

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

func abs(a int) int {
	return max(a, -a)
}

func max4(a, b, c, d int) int {
	return max(max(a, b), max(c, d))
}

func dist(x1, y1, x2, y2 int) int {
	return abs(x1-x2) + abs(y1-y2)
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)
		var a []int
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				a = append(a, max4(dist(i, j, 0, 0), dist(i, j, 0, m-1), dist(i, j, n-1, 0), dist(i, j, n-1, m-1)))
			}
		}
		sort.Ints(a)
		for i := 0; i < n*m; i++ {
			fmt.Fprintf(out, "%d ", a[i])
		}
		fmt.Fprintf(out, "\n")
	}
	out.Flush()
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	_, _ = fmt.Scanf("%d\n", &n)
	for _case := 0; _case < n; _case++ {
		var row, col int
		_, _ = fmt.Scanf("%d %d\n", &row, &col)
		var a = make([]p, row*col)
		for i := 0; i < row; i++ {
			for j := 0; j < col; j++ {
				//fmt.Println(i, j, "=>", i*row+j)
				a[i*col+j] = p{i, j, dd(i, j, row, col)}
			}
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i].d < a[j].d
		})
		for k := 0; k < len(a); k++ {
			if k != 0 {
				fmt.Print(" ")
			}
			fmt.Print(a[k].d)
		}
		fmt.Println()
	}
}

func dd(i, j, row, col int) int {
	a := i + j
	b := row - 1 - i + j
	c := col - 1 - j + i
	d := row - 1 - i + col - 1 - j
	res := a
	if b > res {
		res = b
	}
	if c > res {
		res = c
	}
	if d > res {
		res = d
	}
	return res
}

type p struct {
	x, y, d int
}

package main

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

var (
	reader        = bufio.NewReader(os.Stdin)
	t, n, m, r, c int
)

func max(y []int) int {
	m := y[0]
	for _, x := range y {
		if x > m {
			m = x
		}
	}
	return m
}

func solve(n, m int) string {
	var (
		x []int
		y []string
		t int
	)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			t = max([]int{i + j, n - 1 - i + j, i + m - 1 - j, n - 1 - i + m - 1 - j})
			x = append(x, t)
		}
	}
	sort.Ints(x)

	for i := range x {
		y = append(y, fmt.Sprint(x[i]))
	}
	return strings.Join(y, " ")
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &m)
		fmt.Println(solve(n, m))
	}
}

package main

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

/*
2
5 1
1 5
---
2 3 3 4 4
*/

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	var t = 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var n, m int
		scanf("%d %d\n", &n, &m)

		distances := make([]int, 0, n*m)

		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				// left top, right top, right bottom, left bottom
				maxDist := max(i+j, (n-1-i)+j, (n-1-i)+(m-1-j), i+(m-1-j))
				distances = append(distances, maxDist)
			}
		}

		sort.Ints(distances)

		for _, distance := range distances {
			fmt.Fprintf(writer, "%d ", distance)
		}
		fmt.Fprintln(writer)
	}

}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	maximum := nums[0]
	for _, num := range nums {
		if num > maximum {
			maximum = num
		}
	}
	return maximum
}

//goland:noinspection GoUnusedFunction
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

//goland:noinspection GoUnusedFunction
func isPrime(n int) bool {
	if n == 2 || n == 3 {
		return true
	}

	if n <= 1 || n%2 == 0 || n%3 == 0 {
		return false
	}

	for i := 5; i*i <= n; i += 6 {
		if n%i == 0 || n%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		var n, m int
		fmt.Fscan(in, &n, &m)

		grid := make([]int, n*m)
		var r, c int
		for i := range grid {
			r, c = i/m, i%m
			grid[i] = max(
				max(abs(r-0)+abs(c-0), abs(r-n+1)+abs(c-m+1)),
				max(abs(r-0)+abs(c-m+1), abs(r-n+1)+abs(c-0)))
		}

		sort.Ints(grid)

		lim := n * m
		fmt.Fprint(out, grid[0])
		for i := 1; i < lim; i++ {
			fmt.Fprint(out, " ", grid[i])
		}
		fmt.Fprintln(out)

	}
}

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

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, m int
		fmt.Fscan(in, &n, &m)

		b := make([]int, n*m)

		for i := 0; i < n*m; i++ {
			r := i / m
			c := i % m

			b[i] = max(r, n-r-1) + max(c, m-c-1)
		}

		sort.Ints(b)
		for _, v := range b {
			fmt.Fprintf(out, "%d ", v)
		}
		fmt.Fprintln(out)
	}
}

// generated by 1e9y at 15-01-2022
// template version: 2022-01-11

package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func manhattan(a, b, c, d int) int {
	return abs(a-c) + abs(b-d)
}

func max(a ...int) int {
	out := a[0]
	for _, b := range a[1:] {
		if b > out {
			out = b
		}
	}
	return out
}

func min(a ...int) int {
	out := a[0]
	for _, b := range a[1:] {
		if b < out {
			out = b
		}
	}
	return out
}

// 0000.A
func solve(n, m int) string {
	res := make([]string, n*m)

	// x := n/2 + n%2
	// y := m/2 + m%2
	cc := make([][]int, 0, 4)

	if n%2 == 1 {
		if m%2 == 1 {
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		} else {
			cc = append(cc, []int{n/2 + 1, m / 2})
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		}
	} else {
		if m%2 == 1 {
			cc = append(cc, []int{n / 2, m/2 + 1})
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		} else {
			cc = append(cc, []int{n / 2, m / 2})
			cc = append(cc, []int{n/2 + 1, m / 2})
			cc = append(cc, []int{n / 2, m/2 + 1})
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		}
	}

	maxd := 0
	index := map[int]int{}
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			d := math.MaxInt32
			for _, c := range cc {
				d = min(d, manhattan(i, j, c[0], c[1]))
				maxd = max(maxd, d)
			}
			index[d]++
		}
	}

	i := 0
	kk := 0
	for {
		if _, ok := index[i]; !ok {
			break
		}
		for j := 0; j < index[i]; j++ {
			res[kk] = strconv.Itoa(maxd)
			kk++
		}
		maxd++
		i++
	}

	return strings.Join(res, " ")
}

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

	t := ReadInt(in)
	var n, m int
	for i := 0; i < t; i++ {
		slice := ReadIntSlice(in)
		n, m = slice[0], slice[1]
		Output(solve(n, m))
	}
}

// helpers for convenient input reading

func ReadInt(in *bufio.Reader) int {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	n, _ := strconv.Atoi(line)
	return n
}

func ReadLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	return line
}

func ReadLineParts(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	parts := strings.Split(line, " ")
	return parts
}

func ReadIntSlice(in *bufio.Reader) []int {
	parts := ReadLineParts(in)
	slice := make([]int, len(parts))
	for i, n := range parts {
		slice[i] = MustAtoi(n)
	}
	return slice
}

func ReadInt64Slice(in *bufio.Reader) []int64 {
	parts := ReadLineParts(in)
	slice := make([]int64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseInt(n, 10, 64)
	}
	return slice
}

func ReadUint64Slice(in *bufio.Reader) []uint64 {
	parts := ReadLineParts(in)
	slice := make([]uint64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseUint(n, 10, 64)
	}
	return slice
}

func MustAtoi(s string) int {
	v, _ := strconv.Atoi(s)
	return v
}

func MustParseInt(s string, base, bitSize int) int64 {
	v, _ := strconv.ParseInt(s, base, bitSize)
	return v
}

func MustParseUint(s string, base, bitSize int) uint64 {
	v, _ := strconv.ParseUint(s, base, bitSize)
	return v
}

func SkipLine(in *bufio.Reader) {
	_, _ = in.ReadString('\n')
}

// helper for convenient answer output

func Output(v interface{}) {
	switch v.(type) {
	case bool:
		if v == true {
			Println("YES")
		} else {
			Println("NO")
		}
	default:
		Println(v)
	}
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {

		n := 0
		fmt.Fscan(reader, &n)

		m := 0
		fmt.Fscan(reader, &m)

		out := solve(n, m)
		for i := range out {
			fmt.Fprintf(writter, "%d ", out[i])
		}

		fmt.Fprintln(writter)

		t--
	}

	// n, m := 1, 2
	// fmt.Println(solve(n, m))

}

// 5 4 5
// 4 3 4
// 4 3 4
// 5 4 5
// 334444445555

// 7 6 6 7
// 6 5 5 6
// 5 4 4 5
// 6 5 5 6
// 7 6 6 7
// 44555555666666

// 8 7 6 7 8
// 7 6 5 6 7
// 6 5 4 5 6
// 7 6 5 6 7
// 8 7 6 7 8
// 45555

// 9 8 7 8 9
// 8 7 6 7 8
// 7 6 5 6 7
// 7 6 5 6 7
// 8 7 6 7 8
// 9 8 7 8 9

// 10 9 8 8 9 10
// 9  8 7 7 8 9
// 8  7 6 6 7 8

func solve(n, m int) []int {

	result := []int{}
	begin := n - 1 + m - 1

	for j := 0; j < (n+1)/2; j++ {
		temp := []int{}

		for i := 0; i < (m+1)/2; i++ {
			temp = append(temp, begin-i-j)
		}
		for i := m/2 - 1; i >= 0; i-- {
			temp = append(temp, begin-i-j)
		}

		result = append(result, temp...)
	}

	for j := n/2 - 1; j >= 0; j-- {

		temp := []int{}

		for i := 0; i < (m+1)/2; i++ {
			temp = append(temp, begin-i-j)
		}
		for i := m/2 - 1; i >= 0; i-- {
			temp = append(temp, begin-i-j)
		}

		result = append(result, temp...)
	}

	sort.Ints(result)

	return result
}

func strToint(str string) int {
	num, _ := strconv.Atoi(str)
	return num
}

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

	return b
}

func getFactor(num int) []int {
	result := []int{}

	for i := 1; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			result = append(result, i)
			result = append(result, num/i)
		}
	}

	return result
}

func gcd(a, b int) int {
	if a == 0 {
		return b
	}

	return gcd(b%a, a)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	notSitting()
}

func notSitting() {
	t, n, m := 0, 0, 0
	_, err := fmt.Scan(&t)
	if err != nil {
		return
	}
	for ; t > 0; t-- {
		_, err = fmt.Scan(&n, &m)
		if err != nil {
			return
		}
		ans := make([]int, n*m)
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				a := i - 1 + j - 1
				b := abs(i-n) + j - 1
				c := i - 1 + abs(j-m)
				d := abs(i-n) + abs(j-m)
				ans[(i-1)*m+(j-1)] = max(max(a, b), max(c, d))
			}
		}
		sort.Ints(ans)
		for i, x := range ans {
			c := ' '
			if i == n*m-1 {
				c = '\n'
			}
			fmt.Printf("%d%c", x, c)
		}
	}
}

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

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// generated by 1e9y at 15-01-2022
// template version: 2022-01-11

package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func manhattan(a, b, c, d int) int {
	return abs(a-c) + abs(b-d)
}

func max(a ...int) int {
	out := a[0]
	for _, b := range a[1:] {
		if b > out {
			out = b
		}
	}
	return out
}

func min(a ...int) int {
	out := a[0]
	for _, b := range a[1:] {
		if b < out {
			out = b
		}
	}
	return out
}

// 0000.A
func solve(n, m int) string {
	res := make([]string, n*m)

	// x := n/2 + n%2
	// y := m/2 + m%2
	cc := make([][]int, 0, 4)

	if n%2 == 1 {
		if m%2 == 1 {
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		} else {
			cc = append(cc, []int{n/2 + 1, m / 2})
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		}
	} else {
		if m%2 == 1 {
			cc = append(cc, []int{n / 2, m/2 + 1})
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		} else {
			cc = append(cc, []int{n / 2, m / 2})
			cc = append(cc, []int{n/2 + 1, m / 2})
			cc = append(cc, []int{n/2 + 1, m / 2})
			cc = append(cc, []int{n/2 + 1, m/2 + 1})
		}
	}

	maxd := 0
	index := map[int]int{}
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			d := math.MaxInt32
			for _, c := range cc {
				d = min(d, manhattan(i, j, c[0], c[1]))
				maxd = max(maxd, d)
			}
			index[d]++
		}
	}

	i := 0
	kk := 0
	for {
		if _, ok := index[i]; !ok {
			break
		}
		for j := 0; j < index[i]; j++ {
			res[kk] = strconv.Itoa(maxd)
			kk++
		}
		maxd++
		i++
	}

	return strings.Join(res, " ")
}

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

	t := ReadInt(in)
	var n, m int
	for i := 0; i < t; i++ {
		slice := ReadIntSlice(in)
		n, m = slice[0], slice[1]
		Output(solve(n, m))
	}
}

// helpers for convenient input reading

func ReadInt(in *bufio.Reader) int {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	n, _ := strconv.Atoi(line)
	return n
}

func ReadLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	return line
}

func ReadLineParts(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.Trim(line, "\n\r")
	parts := strings.Split(line, " ")
	return parts
}

func ReadIntSlice(in *bufio.Reader) []int {
	parts := ReadLineParts(in)
	slice := make([]int, len(parts))
	for i, n := range parts {
		slice[i] = MustAtoi(n)
	}
	return slice
}

func ReadInt64Slice(in *bufio.Reader) []int64 {
	parts := ReadLineParts(in)
	slice := make([]int64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseInt(n, 10, 64)
	}
	return slice
}

func ReadUint64Slice(in *bufio.Reader) []uint64 {
	parts := ReadLineParts(in)
	slice := make([]uint64, len(parts))
	for i, n := range parts {
		slice[i] = MustParseUint(n, 10, 64)
	}
	return slice
}

func MustAtoi(s string) int {
	v, _ := strconv.Atoi(s)
	return v
}

func MustParseInt(s string, base, bitSize int) int64 {
	v, _ := strconv.ParseInt(s, base, bitSize)
	return v
}

func MustParseUint(s string, base, bitSize int) uint64 {
	v, _ := strconv.ParseUint(s, base, bitSize)
	return v
}

func SkipLine(in *bufio.Reader) {
	_, _ = in.ReadString('\n')
}

// helper for convenient answer output

func Output(v interface{}) {
	switch v.(type) {
	case bool:
		if v == true {
			Println("YES")
		} else {
			Println("NO")
		}
	default:
		Println(v)
	}
}

package main

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

func main() {

	reader := bufio.NewReader(os.Stdin)
	writter := bufio.NewWriter(os.Stdout)
	defer writter.Flush()

	t := 0
	fmt.Fscan(reader, &t)

	for t > 0 {

		n := 0
		fmt.Fscan(reader, &n)

		m := 0
		fmt.Fscan(reader, &m)

		out := solve(n, m)

		fmt.Fprintln(writter, out)

		t--
	}

	// n, m := 1, 2
	// fmt.Println(solve(n, m))

}

// 5 4 5
// 4 3 4
// 4 3 4
// 5 4 5
// 334444445555

// 7 6 6 7
// 6 5 5 6
// 5 4 4 5
// 6 5 5 6
// 7 6 6 7
// 44555555666666

// 8 7 6 7 8
// 7 6 5 6 7
// 6 5 4 5 6
// 7 6 5 6 7
// 8 7 6 7 8
// 45555

// 9 8 7 8 9
// 8 7 6 7 8
// 7 6 5 6 7
// 7 6 5 6 7
// 8 7 6 7 8
// 9 8 7 8 9

// 10 9 8 8 9 10
// 9  8 7 7 8 9
// 8  7 6 6 7 8

func solve(n, m int) []int {

	result := []int{}
	begin := n - 1 + m - 1

	for j := 0; j < (n+1)/2; j++ {
		temp := []int{}

		for i := 0; i < (m+1)/2; i++ {
			temp = append(temp, begin-i-j)
		}
		for i := m/2 - 1; i >= 0; i-- {
			temp = append(temp, begin-i-j)
		}

		result = append(result, temp...)
	}

	for j := n/2 - 1; j >= 0; j-- {

		temp := []int{}

		for i := 0; i < (m+1)/2; i++ {
			temp = append(temp, begin-i-j)
		}
		for i := m/2 - 1; i >= 0; i-- {
			temp = append(temp, begin-i-j)
		}

		result = append(result, temp...)
	}

	sort.Ints(result)

	return result
}

func strToint(str string) int {
	num, _ := strconv.Atoi(str)
	return num
}

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

	return b
}

func getFactor(num int) []int {
	result := []int{}

	for i := 1; i <= int(math.Sqrt(float64(num))); i++ {
		if num%i == 0 {
			result = append(result, i)
			result = append(result, num/i)
		}
	}

	return result
}

func gcd(a, b int) int {
	if a == 0 {
		return b
	}

	return gcd(b%a, a)
}


// Codeforces 1.12.6
// AtCoder 1.6
// Paiza 1.7.3

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type in struct {
	Scanner *bufio.Scanner
}

func (in *in) s() string {
	in.Scanner.Scan()
	return in.Scanner.Text()
}
func (in *in) i() int64 {
	return in.ibase(10)
}
func (in *in) ibase(base int) int64 {
	i, _ := strconv.ParseInt(in.s(), base, 64)
	return i
}
func (in *in) f() float64 {
	i, _ := strconv.ParseFloat(in.s(), 64)
	return i
}
func (in *in) ss(n int64) []string {
	a := make([]string, n)
	for i := range a {
		a[i] = in.s()
	}
	return a
}
func (in *in) is(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = in.i()
	}
	return a
}
func (in *in) fs(n int64) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = in.f()
	}
	return a
}
func (in *in) imat(h int64, w int64) [][]int64 {
	a := make([][]int64, h)
	for y := range a {
		a[y] = make([]int64, w)
		for x := range a[y] {
			a[y][x] = in.i()
		}
	}
	return a
}

func stdin() in {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	max := 1048576
	buf := make([]byte, max)
	scn.Buffer(buf, max)
	return in{Scanner: scn}
}

type lm struct{}

func (*lm) max(a ...int64) int64 {
	t := a[0]
	for _, v := range a {
		if t < v {
			t = v
		}
	}
	return t
}
func (*lm) min(a ...int64) int64 {
	t := a[0]
	for _, v := range a {
		if t > v {
			t = v
		}
	}
	return t
}
func (*lm) sum(a ...int64) int64 {
	t := int64(0)
	for _, v := range a {
		t += v
	}
	return t
}
func (*lm) abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}
func (lm *lm) gcd(a, b int64) int64 {
	// returns greatest common divisor.
	if a < b {
		a, b = b, a
	}
	if b == 0 {
		return a
	}
	return lm.gcd(b, a%b)
}
func (lm *lm) lcm(a, b int64) int64 {
	// returns least common multiple.
	gcd := lm.gcd(a, b)
	return gcd * (a / gcd) * (b / gcd)
}

type su struct{}

func (*su) iarr(arr []int64) string {
	// int slice to string.
	ret := make([]string, len(arr))
	for i, x := range arr {
		ret[i] = strconv.FormatInt(x, 10)
	}
	return strings.Join(ret, " ")
}
func (*su) ilns(arr []int64) string {
	// int slice to string.
	ret := make([]string, len(arr))
	for i, x := range arr {
		ret[i] = strconv.FormatInt(x, 10)
	}
	return strings.Join(ret, "\n")
}
func (su *su) imat(matrix [][]int64) string {
	// int slice matrix to string.
	ret := make([]string, len(matrix))
	for i := range matrix {
		ret[i] = su.iarr(matrix[i])
	}
	return strings.Join(ret, "\n")
}
func (*su) bool(b bool) str {
	if b {
		return "Yes"
	}
	return "No"
}

type str string

func (s str) upper() string { return strings.ToUpper(string(s)) }
func (s str) lower() string { return strings.ToLower(string(s)) }

type runes []rune

func (p runes) Len() int           { return len(p) }
func (p runes) Less(i, j int) bool { return p[i] < p[j] }
func (p runes) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

type comps [][]int64

func (p comps) Len() int { return len(p) }
func (p comps) Less(i, j int) bool {
	for x := range p[0] {
		// Yes, every row in matrix must have the same length.
		if p[i][x] < p[j][x] {
			return true
		}
	}
	return false
}

func (p comps) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p *comps) Push(arr interface{}) {
	x := arr.([]int64)
	*p = append(*p, x)
}
func (p *comps) Pop() interface{} {
	// https://golang.org/pkg/container/heap/
	var ret []int64
	n := len(*p)
	ret, *p = (*p)[n-1], (*p)[0:n-1]
	return ret
}
func (p *comps) pop() []int64 {
	x := heap.Pop(p).([]int64)
	return x
}

func main() {
	// utilities.
	const zr int64 = int64(0)
	const mod int64 = int64(1000000007)
	in, lm, su := stdin(), lm{}, su{}
	_, _, _ = in, lm, su

	// --- read here.
	q := in.i()
	nm := in.imat(q, 2)

	// --- solve here.
	ans := make([]int64, 0)
	for _, nm := range nm {
		n, m := nm[0], nm[1]
		mp := make(map[int64]int)
		width := zr
		cycleSum := zr
		now := m % 10
		ok := false
		for !ok {
			cycleSum += now
			width++
			mp[now] = 1
			now = (now + m) % 10
			_, ok = mp[now]
			// fmt.Println(mp)
		}
		cycles := n / m / width
		subans := cycleSum * cycles
		for i, j := cycles*width*m+m, zr+1; i <= n; i, j = i+m, j+1 {
			subans += (m * j) % 10
		}

		ans = append(ans, subans)
	}

	printable := su.ilns(ans)
	// --- print here.
	fmt.Println(printable)
}

package main

import (
	"bufio"
	"os"
	"runtime"
	"strconv"
)

/* solution */

var sums [10]int64

func solve() {

	sums[0] = 0
	for m := 1; m < 10; m++ {
		sums[m] = 0
		for i := 1; i < 10; i++ {
			sums[m] += int64((m * i) % 10)
		}
	}

	t := readInt()
	for i := 0; i < t; i++ {
		solveOne(readInt64(), readInt64())
	}

}

func solveOne(n, m int64) {

	k := n / m / 10
	ans := k * sums[m%10]

	for current := k*m*10 + m; current <= n; current += m {
		ans += current % 10
	}

	printInt64(ans)

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

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

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

type in struct {
	Scanner *bufio.Scanner
}

func (in *in) s() string {
	in.Scanner.Scan()
	return in.Scanner.Text()
}
func (in *in) i() int64 {
	return in.ibase(10)
}
func (in *in) ibase(base int) int64 {
	i, _ := strconv.ParseInt(in.s(), base, 64)
	return i
}
func (in *in) f() float64 {
	i, _ := strconv.ParseFloat(in.s(), 64)
	return i
}
func (in *in) ss(n int64) []string {
	a := make([]string, n)
	for i := range a {
		a[i] = in.s()
	}
	return a
}
func (in *in) is(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = in.i()
	}
	return a
}
func (in *in) fs(n int64) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = in.f()
	}
	return a
}
func (in *in) imat(h int64, w int64) [][]int64 {
	a := make([][]int64, h)
	for y := range a {
		a[y] = make([]int64, w)
		for x := range a[y] {
			a[y][x] = in.i()
		}
	}
	return a
}

func stdin() in {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	max := 1048576
	buf := make([]byte, max)
	scn.Buffer(buf, max)
	return in{Scanner: scn}
}

func main() {
	in := stdin()
	t := in.i()
	for i := int64(0); i < t; i++ {
		n, m := in.i(), in.i()
		re := make([]int64, 10)
		r := m % 10
		for j := 0; j < 10; j++ {
			re[j] = int64(j+1) * r % 10
		}
		if r%2 == 0 {
			if r != 0 {
				r = 40
			}
		} else {
			if r == 5 {
				r = 25
			} else {
				r = 45
			}
		}
		ans := int64(n/m/10) * r
		r = n / m % 10
		for j := int64(0); j < r; j++ {
			ans += re[j]
		}
		fmt.Println(ans)
	}
}

/*
8
1 1
10 1
100 3
1024 14
998244353 1337
123 144
1234312817382646 13
60 5
*/

package main

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

func main() {
	sc := NewScanner()
	T := sc.NextInt()
	for p := 0; p < T; p++ {
		N, K := sc.NextInt64(), sc.NextInt64()
		Q := make([]int64, 10)
		tmp := K
		for i := 0; i < 10; i++ {
			Q[tmp%10]++
			tmp += K
		}
		W := int64(N / (K * 10))
		ans := int64(0)
		for i := int64(0); i < 10; i++ {
			ans += i * W * Q[i]
		}
		for i := int64(W * K * 10); i <= N; i += K {
			ans += i % 10
		}
		fmt.Println(ans)

	}

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextFloat() float64 {
	v, _ := strconv.ParseFloat(s.Next(), 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main
import "fmt"
import "bufio"
import "os"

var input *bufio.Reader

func main(){
	input = bufio.NewReader(os.Stdin)
	var T int
	fmt.Fscanf(input, "%d\n", &T)
	for ; T>0; T-- {
		var n,m int64
		fmt.Fscanf(input, "%d%d\n", &n, &m)
		n/=m; m%=10
		arr := make([]int64, 10)
		var sum int64 = 0
		for i:=0; i<10; i++ {
			arr[i] = m*int64(i+1)%10
			sum += arr[i]
		}
		ans := (n/10)*sum
		n%=10
		for i:=1; i<=int(n); i++ {
			ans += arr[i-1];
		}
		fmt.Println(ans)
	}
}

package main

import (
	"fmt"
)

func main() {

	var q int
	fmt.Scanf("%d\n", &q)

	for i := 0; i < q; i++ {
		var n, m int64
		fmt.Scanf("%d %d\n", &n, &m)
		n /= m

		mod := make([]int, 1, 1)
		var s int64

		var j int64
		for j = m; ; j += m {
			var mm = j % m
			if mm == 0 {
				if j%10 == 0 {
					break
				} else {
					mod = append(mod, int(j%10))
					s += j % 10
				}
			}
		}

		r := (n / int64(len(mod))) * s
		for j = 0; j <= n%int64(len(mod)); j++ {
			r += int64(mod[j])
		}
		fmt.Println(r)

	}
}

package main

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

type in struct {
	Scanner *bufio.Scanner
}

func (in *in) s() string {
	in.Scanner.Scan()
	return in.Scanner.Text()
}
func (in *in) i() int64 {
	return in.ibase(10)
}
func (in *in) ibase(base int) int64 {
	i, _ := strconv.ParseInt(in.s(), base, 64)
	return i
}
func (in *in) f() float64 {
	i, _ := strconv.ParseFloat(in.s(), 64)
	return i
}
func (in *in) ss(n int64) []string {
	a := make([]string, n)
	for i := range a {
		a[i] = in.s()
	}
	return a
}
func (in *in) is(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = in.i()
	}
	return a
}
func (in *in) fs(n int64) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = in.f()
	}
	return a
}
func (in *in) imat(h int64, w int64) [][]int64 {
	a := make([][]int64, h)
	for y := range a {
		a[y] = make([]int64, w)
		for x := range a[y] {
			a[y][x] = in.i()
		}
	}
	return a
}

func stdin() in {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	max := 1048576
	buf := make([]byte, max)
	scn.Buffer(buf, max)
	return in{Scanner: scn}
}

func main() {
	in := stdin()
	t := in.i()
	for i := int64(0); i < t; i++ {
		n, m := in.i(), in.i()
		re := make([]int64, 10)
		r := m % 10
		for j := 0; j < 10; j++ {
			re[j] = int64(j+1) * r % 10
		}
		if r%2 == 0 {
			r = 40
		} else {
			r = 45
		}
		ans := int64(n/m/10) * r
		r = n / m % 10
		for j := int64(0); j < r; j++ {
			ans += re[j]
		}
		fmt.Println(ans)
	}
}

// Codeforces 1.12.6
// AtCoder 1.6
// Paiza 1.7.3

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
	"strings"
)

type in struct {
	Scanner *bufio.Scanner
}

func (in *in) s() string {
	in.Scanner.Scan()
	return in.Scanner.Text()
}
func (in *in) i() int64 {
	return in.ibase(10)
}
func (in *in) ibase(base int) int64 {
	i, _ := strconv.ParseInt(in.s(), base, 64)
	return i
}
func (in *in) f() float64 {
	i, _ := strconv.ParseFloat(in.s(), 64)
	return i
}
func (in *in) ss(n int64) []string {
	a := make([]string, n)
	for i := range a {
		a[i] = in.s()
	}
	return a
}
func (in *in) is(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = in.i()
	}
	return a
}
func (in *in) fs(n int64) []float64 {
	a := make([]float64, n)
	for i := range a {
		a[i] = in.f()
	}
	return a
}
func (in *in) imat(h int64, w int64) [][]int64 {
	a := make([][]int64, h)
	for y := range a {
		a[y] = make([]int64, w)
		for x := range a[y] {
			a[y][x] = in.i()
		}
	}
	return a
}

func stdin() in {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	max := 1048576
	buf := make([]byte, max)
	scn.Buffer(buf, max)
	return in{Scanner: scn}
}

type lm struct{}

func (*lm) max(a ...int64) int64 {
	t := a[0]
	for _, v := range a {
		if t < v {
			t = v
		}
	}
	return t
}
func (*lm) min(a ...int64) int64 {
	t := a[0]
	for _, v := range a {
		if t > v {
			t = v
		}
	}
	return t
}
func (*lm) sum(a ...int64) int64 {
	t := int64(0)
	for _, v := range a {
		t += v
	}
	return t
}
func (*lm) abs(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}
func (lm *lm) gcd(a, b int64) int64 {
	// returns greatest common divisor.
	if a < b {
		a, b = b, a
	}
	if b == 0 {
		return a
	}
	return lm.gcd(b, a%b)
}
func (lm *lm) lcm(a, b int64) int64 {
	// returns least common multiple.
	gcd := lm.gcd(a, b)
	return gcd * (a / gcd) * (b / gcd)
}

type su struct{}

func (*su) iarr(arr []int64) string {
	// int slice to string.
	ret := make([]string, len(arr))
	for i, x := range arr {
		ret[i] = strconv.FormatInt(x, 10)
	}
	return strings.Join(ret, " ")
}
func (*su) ilns(arr []int64) string {
	// int slice to string.
	ret := make([]string, len(arr))
	for i, x := range arr {
		ret[i] = strconv.FormatInt(x, 10)
	}
	return strings.Join(ret, "\n")
}
func (su *su) imat(matrix [][]int64) string {
	// int slice matrix to string.
	ret := make([]string, len(matrix))
	for i := range matrix {
		ret[i] = su.iarr(matrix[i])
	}
	return strings.Join(ret, "\n")
}
func (*su) bool(b bool) str {
	if b {
		return "Yes"
	}
	return "No"
}

type str string

func (s str) upper() string { return strings.ToUpper(string(s)) }
func (s str) lower() string { return strings.ToLower(string(s)) }

type runes []rune

func (p runes) Len() int           { return len(p) }
func (p runes) Less(i, j int) bool { return p[i] < p[j] }
func (p runes) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

type comps [][]int64

func (p comps) Len() int { return len(p) }
func (p comps) Less(i, j int) bool {
	for x := range p[0] {
		// Yes, every row in matrix must have the same length.
		if p[i][x] < p[j][x] {
			return true
		}
	}
	return false
}

func (p comps) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p *comps) Push(arr interface{}) {
	x := arr.([]int64)
	*p = append(*p, x)
}
func (p *comps) Pop() interface{} {
	// https://golang.org/pkg/container/heap/
	var ret []int64
	n := len(*p)
	ret, *p = (*p)[n-1], (*p)[0:n-1]
	return ret
}
func (p *comps) pop() []int64 {
	x := heap.Pop(p).([]int64)
	return x
}

func main() {
	// utilities.
	const zr int64 = int64(0)
	const mod int64 = int64(1000000007)
	in, lm, su := stdin(), lm{}, su{}
	_, _, _ = in, lm, su

	// --- read here.
	q := in.i()
	nm := in.imat(q, 2)

	// --- solve here.
	ans := make([]int64, 0)
	for _, nm := range nm {
		n, m := nm[0], nm[1]
		mp := make(map[int64]int)
		width := zr
		cycleSum := zr
		now := m % 10
		ok := false
		for !ok {
			cycleSum += now
			width++
			mp[now] = 1
			now = (now + m) % 10
			_, ok = mp[now]
			// fmt.Println(mp)
		}
		cycles := n / m / width
		subans := cycleSum * cycles
		for i, j := cycles*width*m+m, zr+1; i < n; i, j = i+m, j+1 {
			subans += (m * j) % 10
		}

		ans = append(ans, subans)
	}

	printable := su.ilns(ans)
	// --- print here.
	fmt.Println(printable)
}


package main

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

// github.com/EndlessCheng/codeforces-go
type seg []struct{ l, r, max, from, todo, todoF int }

func (t seg) build(o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		return
	}
	m := (l + r) >> 1
	t.build(o<<1, l, m)
	t.build(o<<1|1, m+1, r)
}

func (t seg) do(o, v, i int) {
	to := &t[o]
	if v > to.todo {
		to.todo, to.todoF = v, i
	}
	if v > to.max {
		to.max, to.from = v, i
	}
}

func (t seg) spread(o int) {
	if v := t[o].todo; v != 0 {
		t.do(o<<1, v, t[o].todoF)
		t.do(o<<1|1, v, t[o].todoF)
		t[o].todo = 0
	}
}

func (t seg) update(o, l, r, v, i int) {
	if l <= t[o].l && t[o].r <= r {
		t.do(o, v, i)
		return
	}
	t.spread(o)
	m := (t[o].l + t[o].r) >> 1
	if l <= m {
		t.update(o<<1, l, r, v, i)
	}
	if m < r {
		t.update(o<<1|1, l, r, v, i)
	}
	lo, ro := t[o<<1], t[o<<1|1]
	t[o].max, t[o].from = max(lo.max, lo.from, ro.max, ro.from)
}

func (t seg) query(o, l, r int) (int, int) {
	if l <= t[o].l && t[o].r <= r {
		return t[o].max, t[o].from
	}
	t.spread(o)
	m := (t[o].l + t[o].r) >> 1
	if r <= m {
		return t.query(o<<1, l, r)
	}
	if m < l {
		return t.query(o<<1|1, l, r)
	}
	a, af := t.query(o<<1, l, r)
	b, bf := t.query(o<<1|1, l, r)
	return max(a, af, b, bf)
}

func CF1557D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct{ l, r int }

	var n, m, row, l, r int
	Fscan(in, &n, &m)
	rs := make([][]pair, n+1)
	x := make([]int, 0, m*2)
	for ; m > 0; m-- {
		Fscan(in, &row, &l, &r)
		rs[row] = append(rs[row], pair{l, r})
		x = append(x, l, r)
	}
	sort.Ints(x)
	kth, k := map[int]int{}, 1
	for i, v := range x {
		if i == 0 || v != x[i-1] {
			kth[v] = k
			k++
		}
	}

	t := make(seg, k*4)
	t.build(1, 1, k)
	fs := make([]int, n+1)
	for i, rs := range rs {
		if rs == nil {
			continue
		}
		mx, from := 0, 0
		for _, p := range rs {
			v, f := t.query(1, kth[p.l], kth[p.r])
			if v > mx {
				mx, from = v, f
			}
		}
		fs[i] = from
		mx++
		for _, p := range rs {
			t.update(1, kth[p.l], kth[p.r], mx, i)
		}
	}
	Fprintln(out, n-t[1].max)
	save := make([]bool, n+1)
	for i := t[1].from; i > 0; i = fs[i] {
		save[i] = true
	}
	for i := 1; i <= n; i++ {
		if !save[i] {
			Fprint(out, i, " ")
		}
	}
}

func main() { CF1557D(os.Stdin, os.Stdout) }

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

package main
import (
    "io"
    "os"
    "fmt"
    "sort"
    "bufio"
)
const N int = 3e5+10
var n, m, tot int
var p, l, r int
var last, visit [N]int
var v sort.IntSlice
var g [][]pair
var lzy, tree [N<<4]pair
type pair struct {
    first, second int
}

func max(x, y pair) pair {
    if x.first > y.first {
        return x
    }
    if x.first == y.first {
        if x.second > y.second {
            return x
        }
    }
    return y
}

func get_pos(x int) int {
    l := 0
    r := tot - 1
    for l <= r {
        mid := (l + r) >> 1
        if v[mid] < x {
            l = mid + 1
        } else {
            r = mid - 1
        }
    }
    return l + 1
}

func push_up(id int) {
    val := max(tree[id<<1], tree[id<<1|1])
    tree[id] = max(tree[id], val)
}

func push_down(id int) {
    if lzy[id].first == 0 {
        return
    }
    lzy[id<<1] = max(lzy[id<<1], lzy[id])
    lzy[id<<1|1] = max(lzy[id<<1|1], lzy[id])
    tree[id<<1] = max(tree[id<<1], lzy[id])
    tree[id<<1|1] = max(tree[id<<1|1], lzy[id])
    lzy[id] = pair{0, 0}
}

func insert(id, l, r, ql, qr int, val pair) {
    if ql <= l && r <= qr {
        tree[id] = max(tree[id], val)
        lzy[id] = max(lzy[id], val)
        return
    }
    push_down(id)
    mid := (l + r) >> 1
    if ql <= mid {
        insert(id<<1, l, mid, ql, qr, val)
    }
    if qr > mid {
        insert(id<<1|1, mid+1, r, ql, qr, val)
    }
    push_up(id)
}

func query(id, l, r, ql, qr int) pair {
    if ql <= l && r <= qr {
        return tree[id]
    }
    push_down(id)
    mid := (l + r) >> 1
    now := pair{0, 0}
    if ql <= mid {
        now = query(id<<1, l, mid, ql, qr)
    }
    if qr > mid {
        now = max(now, query(id<<1|1, mid+1, r, ql, qr))
    }
    push_up(id)
    return now
}

func main() {
    var input io.Reader = bufio.NewReader(os.Stdin)

    fmt.Fscan(input, &n)
    for i := 1; i <= n + 1; i++ {
        tmp := []pair{}
        g = append(g, tmp)
    }

    for fmt.Fscan(input, &m); m > 0; m-- {
        fmt.Fscan(input, &p)
        fmt.Fscan(input, &l)
        fmt.Fscan(input, &r)
        g[p] = append(g[p], pair{l, r})
        v = append(v, l, r)
    }

    sort.Ints(v)
    l = 0
    for i, _ := range v {
        if i == 0 || v[i] != v[i-1] {
            v[l] = v[i]
            l++
        }
    }
    v = v[0:l]
    tot = len(v)

    ans := pair{0, 0}
    for i := 1; i <= n; i++ {
        res := pair{0, 0}
        for _, val := range g[i] {
            l = get_pos(val.first)
            r = get_pos(val.second)
            res = max(res, query(1, 1, tot, l, r))
        }

        last[i] = res.second
        res.first++
        res.second = i
        ans = max(ans, res)

        for _, val := range g[i] {
            l = get_pos(val.first)
            r = get_pos(val.second)
            insert(1, 1, tot, l, r, res)
        }
    }

    pos := ans.second
    for pos > 0 {
        visit[pos] = 1
        pos = last[pos]
    }
    fmt.Println(n - ans.first)
    flag := false
    for i := 1; i <= n; i++ {
        if visit[i] == 0 {
            if flag {
                fmt.Print(" ")
            }
            flag = true
            fmt.Print(i)
        }
    }
}

package main

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

var _, _, _ = main, rw, do

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

type seg struct {
	l, r int
}

func rw(r io.Reader, w io.Writer) {
	var n, m int
	fmt.Fscan(r, &n, &m)
	if n <= 0 {
		return
	}
	a := make([][]seg, n+1)
	for i := 0; i < m; i++ {
		var b seg
		var bi int
		fmt.Fscan(r, &bi, &b.l, &b.r)
		a[bi] = append(a[bi], b)
	}
	w1 := do(n, a)
	fmt.Fprint(w, len(w1))
	fmt.Fprintln(w)
	for i, v := range w1 {
		if i != 0 {
			fmt.Fprint(w, " ")
		}
		fmt.Fprint(w, v)
	}
}

func do(n int, a [][]seg) []int {
	a, l := compress(a)
	t := newSegTree(seg{1, l})
	trace := make([]int, n+1)
	retVal := segVal{}
	for i := 1; i <= n; i++ {
		curVal := segVal{max: 1}
		for _, s := range a[i] {
			if segVal := t.get(s); segVal.max+1 > curVal.max {
				curVal = segVal
				curVal.max++
			}
		}
		trace[i] = curVal.i
		curVal.i = i
		for _, s := range a[i] {
			t.upd(s, curVal)
		}
		if curVal.max > retVal.max {
			retVal = curVal
		}
	}
	return newRet(n, trace, retVal)
}

func newRet(n int, trace []int, retVal segVal) []int {
	vis := make([]bool, n+1)
	for retVal.i != 0 {
		vis[retVal.i] = true
		retVal.i = trace[retVal.i]
	}
	ret := []int{}
	for i := 1; i <= n; i++ {
		if !vis[i] {
			ret = append(ret, i)
		}
	}
	return ret
}

func (node *segNode) upd(s seg, v segVal) {
	if s.l == node.seg.l && s.r == node.seg.r {
		node.v = v
		node.lazy = &node.v
		return
	}
	node.pushDown()
	m := (node.seg.l + node.seg.r) / 2
	if s.r <= m {
		node.l.upd(s, v)
	} else if s.l > m {
		node.r.upd(s, v)
	} else {
		node.l.upd(seg{s.l, m}, v)
		node.r.upd(seg{m + 1, s.r}, v)
	}
	node.pushUp()
}

func (node *segNode) pushDown() {
	if node.lazy == nil {
		return
	}
	for _, child := range []*segNode{node.l, node.r} {
		if child != nil {
			child.v = *node.lazy
			child.lazy = &child.v
		}
	}
	node.lazy = nil
}

func (node *segNode) pushUp() {
	for _, child := range []*segNode{node.l, node.r} {
		if child != nil && child.v.max > node.v.max {
			node.v = child.v
		}
	}
}

func (node *segNode) get(s seg) segVal {
	if s.l == node.seg.l && s.r == node.seg.r {
		return node.v
	}
	node.pushDown()
	var lv, rv segVal
	m := (node.seg.l + node.seg.r) / 2
	if s.r <= m {
		lv = node.l.get(s)
	} else if s.l > m {
		rv = node.r.get(s)
	} else {
		lv = node.l.get(seg{s.l, m})
		rv = node.r.get(seg{m + 1, s.r})
	}
	node.pushUp()
	if lv.max >= rv.max {
		return lv
	}
	return rv
}

type segVal struct {
	max, i int
}

type segNode struct {
	seg  seg
	l, r *segNode
	v    segVal
	lazy *segVal
}

func newSegTree(s seg) *segNode {
	node := &segNode{}
	node.seg = s
	if s.l == s.r {
		return node
	}
	m := (s.l + s.r) / 2
	node.l = newSegTree(seg{s.l, m})
	node.r = newSegTree(seg{m + 1, s.r})
	return node
}

func compress(a [][]seg) ([][]seg, int) {
	m := map[int]int{}
	for _, segs := range a {
		for i := range segs {
			m[segs[i].l] = 0
			m[segs[i].r] = 0
		}
	}
	keys := make([]int, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Ints(keys)
	for i, k := range keys {
		m[k] = i + 1
	}
	for _, segs := range a {
		for i := range segs {
			segs[i].l = m[segs[i].l]
			segs[i].r = m[segs[i].r]
		}
	}
	return a, len(keys)
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var n, m int
	fmt.Fscan(r, &n, &m)
	if n <= 0 {
		return
	}
	var a = make([][][]int, n+1)
	for i := 0; i < m; i++ {
		var j int
		var b [2]int
		fmt.Fscan(r, &j, &b[0], &b[1])
		a[j] = append(a[j], b[:])
	}
	w1 := do(n, a[1:n+1])
	fmt.Fprint(w, len(w1))
	fmt.Fprintln(w)
	for i, v := range w1 {
		if i > 0 {
			fmt.Fprint(w, " ")
		}
		fmt.Fprint(w, v)
	}
}

func do(n int, a [][][]int) []int {
	a, l := compress(n, a)
	t := make([]node, 4*l)
	bt(t, 1, 0, l)
	rf := make([]int, n)
	rm := 0
	ri := -1
	for i, a := range a {
		max := 1
		maxi := -1
		for _, b := range a {
			if v, vi := getMax(t, 1, b[0], b[1]); v+1 > max {
				max, maxi = v+1, vi
			}
		}
		rf[i] = maxi
		for _, b := range a {
			upd(t, 1, b[0], b[1], max, i)
		}
		if max > rm {
			rm, ri = max, i
		}
	}
	// for i := range a {
	// 	fmt.Println(i+1, a[i], rf[i]+1, rm, ri+1)
	// }
	ra := make([]bool, n)
	for ri != -1 {
		ra[ri] = true
		ri = rf[ri]
	}
	ret := []int{}
	for i := 0; i < n; i++ {
		if !ra[i] {
			ret = append(ret, i+1)
		}
	}
	return ret
}

func pushDown(t []node, i int) {
	if v, vi := t[i].lazyV, t[i].lazyVi; v > 0 {
		t[i].lazyV = 0
		t[i*2].v = v
		t[i*2].lazyV = v
		t[i*2+1].v = v
		t[i*2+1].lazyV = v

		t[i].lazyVi = 0
		t[i*2].vi = vi
		t[i*2].lazyVi = vi
		t[i*2+1].vi = vi
		t[i*2+1].lazyVi = vi
	}
}

func pushUp(t []node, i int) {
	if t[i*2].v > t[i].v {
		t[i].v = t[i*2].v
		t[i].vi = t[i*2].vi
	}
	if t[i*2+1].v > t[i].v {
		t[i].v = t[i*2+1].v
		t[i].vi = t[i*2+1].vi
	}
}

func getMax(t []node, i, l, r int) (int, int) {
	if l == t[i].l && t[i].r == r {
		return t[i].v, t[i].vi
	}
	pushDown(t, i)
	m := (t[i].l + t[i].r) / 2
	var lv, rv, lvi, rvi int
	if r <= m {
		lv, lvi = getMax(t, i*2, l, r)
	} else if l > m {
		rv, rvi = getMax(t, i*2+1, l, r)
	} else {
		lv, lvi = getMax(t, i*2, l, m)
		rv, rvi = getMax(t, i*2+1, m+1, r)
	}
	pushUp(t, i)
	if lv >= rv {
		return lv, lvi
	}
	return rv, rvi
}

func upd(t []node, i, l, r, v, vi int) {
	if t[i].l == l && r == t[i].r {
		t[i].v = v
		t[i].vi = vi
		t[i].lazyV = v
		t[i].lazyVi = vi
		return
	}
	pushDown(t, i)
	m := (t[i].l + t[i].r) / 2
	if r <= m {
		upd(t, i*2, l, r, v, vi)
	} else if l > m {
		upd(t, i*2+1, l, r, v, vi)
	} else {
		upd(t, i*2, l, m, v, vi)
		upd(t, i*2+1, m+1, r, v, vi)
	}
	pushUp(t, i)
}

type node struct {
	l, r, v, vi, lazyV, lazyVi int
}

func bt(t []node, i, l, r int) {
	t[i].l = l
	t[i].r = r
	t[i].vi = -1
	if l == r {
		return
	}
	m := (l + r) / 2
	bt(t, i*2, l, m)
	bt(t, i*2+1, m+1, r)
}

func compress(n int, a [][][]int) ([][][]int, int) {
	m := map[int]int{}
	for _, a := range a {
		for _, b := range a {
			m[b[0]] = 0
			m[b[1]] = 0
		}
	}
	keys := make([]int, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Ints(keys)
	for i, v := range keys {
		m[v] = i
	}
	for _, a := range a {
		for _, b := range a {
			b[0] = m[b[0]]
			b[1] = m[b[1]]
		}
	}
	return a, len(keys) - 1
}

package main

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

var _, _, _ = main, rw, do

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

type seg struct {
	l, r int
}

func rw(r io.Reader, w io.Writer) {
	var n, m int
	fmt.Fscan(r, &n, &m)
	if n <= 0 {
		return
	}
	a := make([][]seg, n+1)
	for i := 0; i < m; i++ {
		var b seg
		var bi int
		fmt.Fscan(r, &bi, &b.l, &b.r)
		a[bi] = append(a[bi], b)
	}
	w1 := do(n, a)
	fmt.Fprint(w, len(w1))
	fmt.Fprintln(w)
	for i, v := range w1 {
		if i != 0 {
			fmt.Fprint(w, " ")
		}
		fmt.Fprint(w, v)
	}
}

func do(n int, a [][]seg) []int {
	a, l := compress(a)
	t := newSegTree(1, l)
	trace := make([]int, n+1)
	retVal := segVal{}
	for i := 1; i <= n; i++ {
		curVal := segVal{max: 1}
		for _, seg := range a[i] {
			if segVal := t.get(seg.l, seg.r); segVal.max+1 > curVal.max {
				curVal = segVal
				curVal.max++
			}
		}
		trace[i], curVal.i = curVal.i, i
		for _, seg := range a[i] {
			t.upd(seg.l, seg.r, curVal)
		}
		if curVal.max > retVal.max {
			retVal = curVal
		}
	}
	return newRet(n, trace, retVal)
}

func newRet(n int, trace []int, retVal segVal) []int {
	vis := make([]bool, n+1)
	for retVal.i != 0 {
		vis[retVal.i] = true
		retVal.i = trace[retVal.i]
	}
	ret := []int{}
	for i := 1; i <= n; i++ {
		if !vis[i] {
			ret = append(ret, i)
		}
	}
	return ret
}

func (node *segNode) upd(l, r int, v segVal) {
	if l == node.l && r == node.r {
		node.v = v
		node.lazy = &node.v
		return
	}
	node.pushDown()
	m := (node.l + node.r) / 2
	if r <= m {
		node.lnode.upd(l, r, v)
	} else if l > m {
		node.rnode.upd(l, r, v)
	} else {
		node.lnode.upd(l, m, v)
		node.rnode.upd(m+1, r, v)
	}
	node.pushUp()
}

func (node *segNode) pushDown() {
	if node.lazy == nil {
		return
	}
	for _, child := range []*segNode{node.lnode, node.rnode} {
		if child != nil {
			child.v = *node.lazy
			child.lazy = &child.v
		}
	}
}

func (node *segNode) pushUp() {
	for _, child := range []*segNode{node.lnode, node.rnode} {
		if child != nil && child.v.max > node.v.max {
			node.v = child.v
		}
	}
}

func (node *segNode) get(l, r int) segVal {
	if l == node.l && r == node.r {
		return node.v
	}
	node.pushDown()
	var lv, rv segVal
	m := (node.l + node.r) / 2
	if r <= m {
		lv = node.lnode.get(l, r)
	} else if l > m {
		rv = node.rnode.get(l, r)
	} else {
		lv = node.lnode.get(l, m)
		rv = node.rnode.get(m+1, r)
	}
	node.pushUp()
	if lv.max >= rv.max {
		return lv
	}
	return rv
}

type segVal struct {
	max, i int
}

type segNode struct {
	l, r         int
	lnode, rnode *segNode
	v            segVal
	lazy         *segVal
}

func newSegTree(l, r int) *segNode {
	node := &segNode{}
	node.l = l
	node.r = r
	if l == r {
		return node
	}
	m := (l + r) / 2
	node.lnode = newSegTree(l, m)
	node.rnode = newSegTree(m+1, r)
	return node
}

func compress(a [][]seg) ([][]seg, int) {
	m := map[int]int{}
	for _, segs := range a {
		for i := range segs {
			m[segs[i].l] = 0
			m[segs[i].r] = 0
		}
	}
	keys := make([]int, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Ints(keys)
	for i, k := range keys {
		m[k] = i + 1
	}
	for _, segs := range a {
		for i := range segs {
			segs[i].l = m[segs[i].l]
			segs[i].r = m[segs[i].r]
		}
	}
	return a, len(keys)
}

package main

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

var _, _, _ = main, rw, do

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

func rw(r io.Reader, w io.Writer) {
	var n, m int
	fmt.Fscan(r, &n, &m)
	if n <= 0 {
		return
	}
	var a = make([][][]int, n+1)
	for i := 0; i < m; i++ {
		var j int
		var b [2]int
		fmt.Fscan(r, &j, &b[0], &b[1])
		a[j] = append(a[j], b[:])
	}
	w1 := do(n, a[1:n+1])
	fmt.Fprint(w, len(w1))
	fmt.Fprintln(w)
	for i, v := range w1 {
		if i > 0 {
			fmt.Fprint(w, " ")
		}
		fmt.Fprint(w, v)
	}
}

func do(n int, a [][][]int) []int {
	a, l := compress(n, a)
	t := make([]node, 4*l)
	bt(t, 1, 0, l)
	rf := make([]int, n)
	rm := 0
	ri := -1
	for i, a := range a {
		max := 1
		maxi := -1
		for _, b := range a {
			if v, vi := getMax(t, 1, b[0], b[1]); v+1 > max && vi != i {
				max, maxi = v+1, vi
			}
		}
		rf[i] = maxi
		for _, b := range a {
			upd(t, 1, b[0], b[1], max, i)
		}
		if max > rm {
			rm, ri = max, i
		}
	}
	// fmt.Println(rm, ri, rf)
	ra := make([]bool, n)
	for ri != -1 {
		ra[ri] = true
		ri = rf[ri]
	}
	ret := []int{}
	for i := 0; i < n; i++ {
		if !ra[i] {
			ret = append(ret, i+1)
		}
	}
	return ret
}

func push(t []node, i int) {
	if v, vi := t[i].lazyV, t[i].lazyVi; v > 0 {
		t[i].lazyV = 0
		t[i*2].v = v
		t[i*2].lazyV = v
		t[i*2+1].v = v
		t[i*2+1].lazyV = v

		t[i].lazyVi = 0
		t[i*2].vi = vi
		t[i*2].lazyVi = vi
		t[i*2+1].vi = vi
		t[i*2+1].lazyVi = vi
	}
}

func getMax(t []node, i, l, r int) (int, int) {
	if l == t[i].l && t[i].r == r {
		return t[i].v, t[i].vi
	}
	push(t, i)
	m := (t[i].l + t[i].r) / 2
	var lv, rv, lvi, rvi int
	if r <= m {
		lv, lvi = getMax(t, i*2, l, r)
	} else if l > m {
		rv, rvi = getMax(t, i*2+1, l, r)
	} else {
		lv, lvi = getMax(t, i*2, l, m)
		rv, rvi = getMax(t, i*2+1, m+1, r)
	}
	if lv >= rv {
		return lv, lvi
	}
	return rv, rvi
}

func upd(t []node, i, l, r, v, vi int) {
	if t[i].l == l && r == t[i].r {
		t[i].v = v
		t[i].vi = vi
		t[i].lazyV = v
		t[i].lazyVi = vi
		return
	}
	push(t, i)
	m := (t[i].l + t[i].r) / 2
	if r <= m {
		upd(t, i*2, l, r, v, vi)
	} else if l > m {
		upd(t, i*2+1, l, r, v, vi)
	} else {
		upd(t, i*2, l, m, v, vi)
		upd(t, i*2+1, m+1, r, v, vi)
	}
}

type node struct {
	l, r, v, vi, lazyV, lazyVi int
}

func bt(t []node, i, l, r int) {
	t[i].l = l
	t[i].r = r
	t[i].vi = -1
	if l == r {
		return
	}
	m := (l + r) / 2
	bt(t, i*2, l, m)
	bt(t, i*2+1, m+1, r)
}

func compress(n int, a [][][]int) ([][][]int, int) {
	m := map[int]int{}
	for _, a := range a {
		for _, b := range a {
			m[b[0]] = 0
			m[b[1]] = 0
		}
	}
	keys := make([]int, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	sort.Ints(keys)
	for i, v := range keys {
		m[v] = i
	}
	for _, a := range a {
		for _, b := range a {
			b[0] = m[b[0]]
			b[1] = m[b[1]]
		}
	}
	return a, len(keys) - 1
}

package main

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

var _, _, _ = main, rw, do

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

type seg struct {
	y, x1, x2 int
}

func rw(r io.Reader, w io.Writer) {
	var n, m int
	var a = make([]seg, 3e5+5)
	fmt.Fscan(r, &n, &m)
	for i := 0; i < m; i++ {
		b := &a[i]
		fmt.Fscan(r, &b.y, &b.x1, &b.x2)
	}
	w1 := do(n, a[:m])
	fmt.Fprint(w, len(w1))
	fmt.Fprintln(w)
	for i, v := range w1 {
		if i > 0 {
			fmt.Fprint(w, " ")
		}
		fmt.Fprint(w, v)
	}
}

func do(n int, a []seg) []int {
	mx := map[int]struct{}{}
	mx1 := map[int][]int{}
	mx2 := map[int][]int{}
	for i, b := range a {
		mx[b.x1] = struct{}{}
		mx[b.x2] = struct{}{}
		mx1[b.x1] = append(mx1[b.x1], i)
		mx2[b.x2] = append(mx2[b.x2], i)
	}
	keys := make([]int, 0, len(mx))
	for k := range mx {
		keys = append(keys, k)
	}
	m := map[int]struct{}{}
	rm := map[int]map[int]struct{}{}
	for _, x := range keys {
		for _, i := range mx1[x] {
			m[a[i].y] = struct{}{}
		}

		ys := make([]int, 0, len(m))
		for y := range m {
			ys = append(ys, y)
		}
		sort.Ints(ys)
		for i := 0; i < len(ys)-1; i++ {
			if rm[ys[i]] == nil {
				rm[ys[i]] = map[int]struct{}{}
			}
			rm[ys[i]][ys[i+1]] = struct{}{}
		}

		for _, i := range mx2[x] {
			delete(m, a[i].y)
		}
	}
	d := map[int]int{}
	f := map[int]int{}
	for i := 1; i <= n; i++ {
		dfs(i, rm, d, f)
	}
	maxd := 0
	maxi := 0
	for i := 1; i <= n; i++ {
		if d[i] > maxd {
			maxd, maxi = d[i], i
		}
	}
	maxa := make([]bool, n+1)
	for maxi != -1 {
		maxa[maxi] = true
		maxi = f[maxi]
	}
	ra := []int{}
	for i := 1; i <= n; i++ {
		if !maxa[i] {
			ra = append(ra, i)
		}
	}
	return ra
}

func dfs(i int, rm map[int]map[int]struct{}, d, f map[int]int) {
	if d[i] > 0 {
		return
	}
	d[i], f[i] = 1, -1
	for j := range rm[i] {
		dfs(j, rm, d, f)
		if v := d[j] + 1; v > d[i] {
			d[i], f[i] = v, j
		}
	}
}

package main
import (
    "io"
    "os"
    "fmt"
    "sort"
    "bufio"
)
const N int = 3e5+10
var n, m, tot int
var p, l, r int
var last, visit [N]int
var v sort.IntSlice
var g [][]pair
var lzy, tree [N<<4]pair
type pair struct {
    first, second int
}
func p_max(x, y pair) pair {
    if x.first > y.first {
        return x
    }
    if x.first == y.first {
        if x.second > y.second {
            return x
        }
    }
    return y
}

func get_pos(x int) int {
    l := 0
    r := tot - 1
    for l <= r {
        mid := (l + r) >> 1
        if mid < x {
            l = mid + 1
        } else {
            r = mid - 1
        }
    }
    return l + 1
}

func push_up(id int) {
    val := p_max(tree[id<<1], tree[id<<1|1])
    tree[id] = p_max(tree[id], val)
}

func push_down(id int) {
    if lzy[id].first == 0 {
        return
    }
    lzy[id<<1] = p_max(lzy[id<<1], lzy[id])
    lzy[id<<1|1] = p_max(lzy[id<<1|1], lzy[id])
    tree[id<<1] = p_max(tree[id<<1], lzy[id])
    tree[id<<1|1] = p_max(tree[id<<1|1], lzy[id])
    lzy[id] = pair{0, 0}
}

func insert(id, l, r, ql, qr int, val pair) {
    if ql <= l && r <= qr {
        tree[id] = p_max(tree[id], val)
        lzy[id] = p_max(lzy[id], val)
        return
    }
    push_down(id)
    mid := (l + r) >> 1
    if ql <= mid {
        insert(id<<1, l, mid, ql, qr, val)
    }
    if qr > mid {
        insert(id<<1|1, mid+1, r, ql, qr, val)
    }
    push_up(id)
}

func query(id, l, r, ql, qr int) pair {
    if ql <= l && r <= qr {
        return tree[id]
    }
    push_down(id)
    mid := (l + r) >> 1
    now := pair{0, 0}
    if ql <= mid {
        now = query(id<<1, l, mid, ql, qr)
    }
    if qr > mid {
        now = p_max(now, query(id<<1|1, mid+1, r, ql, qr))
    }
    push_up(id)
    return now
}

func main() {
    var input io.Reader = bufio.NewReader(os.Stdin)

    fmt.Fscan(input, &n)
    for i := 1; i <= n + 1; i++ {
        tmp := []pair{}
        g = append(g, tmp)
    }

    for fmt.Fscan(input, &m); m > 0; m-- {
        fmt.Fscan(input, &p)
        fmt.Fscan(input, &l)
        fmt.Fscan(input, &r)
        g[p] = append(g[p], pair{l, r})
        v = append(v, l, r)
    }

    sort.Ints(v)
    l = 0
    for i, _ := range v {
        if i == 0 || v[i] != v[i-1] {
            v[l] = v[i]
            l++
        }
    }
    v = v[0:l]
    tot = len(v)

    ans := pair{0, 0}
    for i := 1; i <= n; i++ {
        res := pair{0, 0}
        for _, val := range g[i] {
            l = get_pos(val.first)
            r = get_pos(val.second)
            res = p_max(res, query(1, 1, tot, l, r))
        }

        last[i] = res.second
        res.first++
        res.second = i
        ans = p_max(ans, res)

        for _, val := range g[i] {
            l = get_pos(val.first)
            r = get_pos(val.second)
            insert(1, 1, tot, l, r, res)
        }
    }

    pos := ans.second
    for pos > 0 {
        visit[pos] = 1
        pos = last[pos]
    }
    fmt.Println(n - ans.first)
    flag := false
    for i := 1; i <= n; i++ {
        if visit[i] == 0 {
            if flag {
                fmt.Print(" ")
            }
            flag = true
            fmt.Print(i)
        }
    }
}

package main
import (
    "io"
    "os"
    "fmt"
    "sort"
    "bufio"
)
const N int = 3e5+10
var n, m, tot int
var p, l, r int
var last, visit [N]int
var v sort.IntSlice
var g [][]pair
var lzy, tree [N<<4]pair
type pair struct {
    first, second int
}
func max(x, y pair) pair {
    if x.first > y.first {
        return x
    }
    if x.first == y.first {
        if x.second > y.second {
            return x
        }
    }
    return y
}

func get_pos(x int) int {
    l := 0
    r := tot - 1
    for l <= r {
        mid := (l + r) >> 1
        if mid < x {
            l = mid + 1
        } else {
            r = mid - 1
        }
    }
    return l + 1
}

func push_up(id int) {
    val := max(tree[id<<1], tree[id<<1|1])
    tree[id] = max(tree[id], val)
}

func push_down(id int) {
    if lzy[id].first == 0 {
        return
    }
    lzy[id<<1] = max(lzy[id<<1], lzy[id])
    lzy[id<<1|1] = max(lzy[id<<1|1], lzy[id])
    tree[id<<1] = max(tree[id<<1], lzy[id])
    tree[id<<1|1] = max(tree[id<<1|1], lzy[id])
    lzy[id] = pair{0, 0}
}

func insert(id, l, r, ql, qr int, val pair) {
    if ql <= l && r <= qr {
        tree[id] = max(tree[id], val)
        lzy[id] = max(lzy[id], val)
        return
    }
    push_down(id)
    mid := (l + r) >> 1
    if ql <= mid {
        insert(id<<1, l, mid, ql, qr, val)
    }
    if qr > mid {
        insert(id<<1|1, mid+1, r, ql, qr, val)
    }
    push_up(id)
}

func query(id, l, r, ql, qr int) pair {
    if ql <= l && r <= qr {
        return tree[id]
    }
    push_down(id)
    mid := (l + r) >> 1
    now := pair{0, 0}
    if ql <= mid {
        now = query(id<<1, l, mid, ql, qr)
    }
    if qr > mid {
        now = max(now, query(id<<1|1, mid+1, r, ql, qr))
    }
    push_up(id)
    return now
}

func main() {
    var input io.Reader = bufio.NewReader(os.Stdin)
    fmt.Fscan(input, &n)

    for i := 1; i <= n + 1; i++ {
        var tmp = []pair{}
        g = append(g, tmp)
    }
    for fmt.Fscan(input, &m); m > 0; m-- {
        fmt.Fscan(input, &p)
        fmt.Fscan(input, &l)
        fmt.Fscan(input, &r)
        g[p] = append(g[p], pair{l, r})
        v = append(v, l, r)
    }
    sort.Ints(v)
    l = 0
    for i, _ := range v {
        if i == 0 || v[i] != v[i-1] {
            v[l] = v[i]
            l++
        }
    }
    v = v[0:l]
    tot = len(v)
    ans := pair{0, 0}
    for i := 1; i <= n; i++ {
        res := pair{0, 0}
        for _, val := range g[i] {
            l = get_pos(val.first)
            r = get_pos(val.second)
            res = max(res, query(1, 1, tot, l, r))
        }

        last[i] = res.second
        res.first++
        res.second = i
        ans = max(ans, res)

        for _, val := range g[i] {
            l = get_pos(val.first)
            r = get_pos(val.second)
            insert(1, 1, tot, l, r, res)
        }
    }

    pos := ans.second
    for pos > 0 {
        visit[pos] = 1
        pos = last[pos]
    }
    fmt.Println(n - ans.first)
    flag := false
    for i := 1; i <= n; i++ {
        if visit[i] == 0 {
            if flag {
                fmt.Print(" ")
            }
            flag = true
            fmt.Print(i)
        }
    }
}