There is a house with $$$n$$$ flats situated on the main street of Berlatov. Vova is watching this house every night. The house can be represented as an array of $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i = 1$$$ if in the $$$i$$$-th flat the light is on and $$$a_i = 0$$$ otherwise.Vova thinks that people in the $$$i$$$-th flats are disturbed and cannot sleep if and only if $$$1 < i < n$$$ and $$$a_{i - 1} = a_{i + 1} = 1$$$ and $$$a_i = 0$$$.Vova is concerned by the following question: what is the minimum number $$$k$$$ such that if people from exactly $$$k$$$ pairwise distinct flats will turn off the lights then nobody will be disturbed? Your task is to find this number $$$k$$$.

The first line of the input contains one integer $$$n$$$ ($$$3 \le n \le 100$$$) — the number of flats in the house. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$a_i \in \{0, 1\}$$$), where $$$a_i$$$ is the state of light in the $$$i$$$-th flat.

Print only one integer — the minimum number $$$k$$$ such that if people from exactly $$$k$$$ pairwise distinct flats will turn off the light then nobody will be disturbed.

NoteIn the first example people from flats $$$2$$$ and $$$7$$$ or $$$4$$$ and $$$7$$$ can turn off the light and nobody will be disturbed. It can be shown that there is no better answer in this example.There are no disturbed people in second and third examples.

package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scan(&n)
    arr := make([]int, n)
    res := 0
    for i := 0; i < n; i++ {
        fmt.Scan(&arr[i])
    }
    for i := 1; i < n - 1; i++ {
        if arr[i] == 0 && arr[i - 1] == 1 && arr[i + 1] == 1 {
            res++
            arr[i - 1] = 0
            arr[i + 1] = 0
        }
    }
    fmt.Println(res)
}

package main

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

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

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

func main() {
	defer writer.Flush()

	var n int64
	scanf("%d\n", &n)
	a := make([]int, n)
	for i := range a {
		scanf("%d", &a[i])
	}

	// 	printf("%v\n", a)
	sw := 0
	for i := 1; i < len(a)-1; i++ {
		if a[i-1] == 1 && a[i] == 0 && a[i+1] == 1 {
			a[i+1] = 0
			sw++
		}
	}
	printf("%d", sw)
}

package main

import "fmt"

func main() {

	var n int

	fmt.Scan(&n)

	var v []int

	for i := 0; i < n; i++ {
		var val int
		fmt.Scan(&val)
		v = append(v, val)
	}

	resp := 0

	for i := 1; i < len(v)-1; i++ {
		if v[i] == 0 && v[i-1] == 1 && v[i+1] == 1 {
			v[i+1] = 0
			resp++
		}
	}

	fmt.Println(resp)
}

package main

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

func IsDisturbed(loc int, arr []int) bool {
    if loc > 0 && loc < len(arr) - 1 &&
        arr[loc] == 0 && arr[loc-1] == 1 && arr[loc+1] == 1 {
        return true
    }

    return false
}

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

    var n int
    fmt.Fscan(stdin, &n)
    var a []int = make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Fscan(stdin, &a[i])
    }

    var cnt int = 0
    for i := 1; i < n - 1; {
        if IsDisturbed(i, a) {
            if IsDisturbed(i + 2, a) {
                i += 2
            }
            cnt++
        }
        i++
    }

    fmt.Println(cnt)
}

// Codeforces 1077 B Disturbed People train
package main

import (
	"bufio"
	"fmt"
	"strings"
	"strconv"
	"os"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
	var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for i:=0; i<len(ss); i++ {
		a[i], err = strconv.Atoi(ss[i])
		if err != nil {
			fmt.Println("n atoi failure", err)
			panic(err)
		}
	}
	return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
	a := make([]int, n)
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
	for j:=0; j<n; j++ {
		aa,err := strconv.Atoi(ss[j])
		if err != nil {
			fmt.Println("aa conv failed", err)
			panic(err)
		}
		a[j] = aa
	}
	return a
}

var n int
var a []int
var di []bool
var mink int
var ii int

func readData() {
	in = bufio.NewReader(os.Stdin)
	// reading n, k
	n, _, _, _, _ = readFive()
	// reading a
	a = readLineNumbs(n)
}

func diNext() bool {
	return ii<n-1 && di[ii+1]
}
func diPrev() bool {
	return ii>0 && di[ii-1]
}

func solve() {
	// prepare disturbed
	di = make([]bool, n)
	for i:=0; i<n; i++ {
		if i>0 && i<n-1 && a[i-1]==1 && a[i+1]==1 && a[i]==0 {
			di[i] = true
		}
	}
	//fmt.Println("di:", di)
	// Cl - clear before, Ncl - not cleared before, d b Ncl<=Cl
	minkCl,minkNcl := 0,0
	for ii=0; ii<n; ii++ {
		if a[ii] != 1 {
			continue
		}
		if diNext() && !diPrev() {
			// before disturbed
			minkCl++
		} else if !diNext() && diPrev() {
			minkNcl = minkCl
		} else if diNext() && diPrev() {
			oCl := minkCl
			minkCl = minkNcl+1
			minkNcl = oCl
		} else {
			// do nothing
		}
		//fmt.Printf("ii,prev,next = %d %v %v Cl,Ncl = %d %d\n", ii,diPrev(),diNext(),minkCl,minkNcl)
	}
	mink = minkCl
}

func printRes() {
	fmt.Println(mink)
}

func main() {
	readData()
	solve()
	printRes()
}

package main

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

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

	apts := make([]int, apt)
	for i := 0; i < apt; i++ {
		fmt.Fscan(reader, &apts[i])
	}
	var count int
	for i := 1; i < apt-1; i++ {
		if apts[i] == 0 && apts[i+1] == apts[i-1] && apts[i-1] == 1 {
			count, apts[i+1] = count+1, 0
		}
	}

	fmt.Println(count)
}

package main

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

var scanner = bufio.NewScanner(os.Stdin)

func init() {
	scanner.Split(bufio.ScanWords)
}

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

func scanBytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

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

func scanInt() int {
	scanner.Scan()
	v, _ := strconv.Atoi(scanner.Text())
	return v
}

func scanInts(n int) []int {
	l := make([]int, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		l[i], _ = strconv.Atoi(scanner.Text())
	}

	return l
}

func scanInts64(n int64) []int64 {
	l := make([]int64, n)
	for i := int64(0); i < n; i++ {
		scanner.Scan()
		l[i], _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}

	return l
}

func scanBool() bool {
	scanner.Scan()
	v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
	return v == 1
}

func scanBools(n int64) []bool {
	l := make([]bool, n)
	for i := int64(0); i < n; i++ {
		scanner.Scan()
		v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		l[i] = v == 1
	}

	return l
}

func main() {
	n := scanInt()
	s := scanBools(int64(n))

	cnt := 0
	for i := 2; i < n; i++ {
		if s[i-2] && !s[i-1] && s[i] {
			s[i] = false
			cnt++
		}
	}

	fmt.Println(cnt)
}

func abs(a int64) int64 {
	if a >= 0 {
		return a
	}

	return -a
}

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

	return b
}

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

	return b
}

package main

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

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

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

func main() {
	defer writer.Flush()

	var n int64
	scanf("%d\n", &n)
	a := make([]int, n)
	for i := range a {
		scanf("%d", &a[i])
	}

	// 	printf("%v\n", a)
	sw := 0
	for i := 1; i < len(a)-2; i++ {
		if a[i-1] == 1 && a[i] == 0 && a[i+1] == 1 {
			a[i+1] = 0
			sw++
		}
	}
	printf("%d", sw)
}


Vasya has two arrays $$$A$$$ and $$$B$$$ of lengths $$$n$$$ and $$$m$$$, respectively.He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $$$[1, 10, 100, 1000, 10000]$$$ Vasya can obtain array $$$[1, 1110, 10000]$$$, and from array $$$[1, 2, 3]$$$ Vasya can obtain array $$$[6]$$$.Two arrays $$$A$$$ and $$$B$$$ are considered equal if and only if they have the same length and for each valid $$$i$$$ $$$A_i = B_i$$$.Vasya wants to perform some of these operations on array $$$A$$$, some on array $$$B$$$, in such a way that arrays $$$A$$$ and $$$B$$$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible.Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $$$A$$$ and $$$B$$$ equal.

The first line contains a single integer $$$n~(1 \le n \le 3 \cdot 10^5)$$$ — the length of the first array. The second line contains $$$n$$$ integers $$$a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)$$$ — elements of the array $$$A$$$. The third line contains a single integer $$$m~(1 \le m \le 3 \cdot 10^5)$$$ — the length of the second array. The fourth line contains $$$m$$$ integers $$$b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)$$$ - elements of the array $$$B$$$.

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays $$$A$$$ and $$$B$$$ in such a way that they became equal. If there is no way to make array equal, print "-1".

package main

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

var n, m int
var a, b []int64
var sums map[int64]bool

func main() {
	buf := bufio.NewReader(os.Stdin)
	sums = make(map[int64]bool)
	fmt.Fscanf(buf, "%d\n", &n)
	s := int64(0)
	for i := 0; i < n; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		sums[s] = true
	}
	s1 := s
	cnt := 0
	fmt.Fscanf(buf, "\n%d\n", &m)
	s = 0
	for i := 0; i < m; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		if sums[s] {
			cnt++
		}
	}
	if cnt == 0 || s1 != s {
		cnt = -1
	}
	fmt.Println(cnt)

}

package main

import."fmt"
import"bufio"
import"os"

func main(){
	in:=bufio.NewReader(os.Stdin)
	var n,m int
	Fscan(in,&n)
	a:=map[int64]bool{}
	sumA:=int64(0)
	for i:=0;i<n;i++{
		var aI int64
		Fscan(in,&aI)
		sumA+=aI
		a[sumA]=true
	}
	Fscan(in,&m)
	sumB:=int64(0)
	ans:=0
	for i:=0;i<m;i++{
		var bI int64
		Fscan(in,&bI)
		sumB+=bI
		if a[sumB]{
			ans++
		}
	}

	if sumA==sumB{
		Print(ans)
	}else{
		Print(-1)
	}
}

package main

import."fmt"
import"bufio"
import"os"

func main(){
	in:=bufio.NewReader(os.Stdin)
	var n,m int
	Fscan(in,&n)
	a:=map[int64]struct{}{}
	sumA:=int64(0)
	for i:=0;i<n;i++{
		var aI int64
		Fscan(in,&aI)
		sumA+=aI
		a[sumA]=struct{}{}
	}
	Fscan(in,&m)
	sumB:=int64(0)
	ans:=0
	for i:=0;i<m;i++{
		var bI int64
		Fscan(in,&bI)
		sumB+=bI
		_,ok:=a[sumB]
		if ok{
			ans++
		}
	}

	if sumA==sumB{
		Print(ans)
	}else{
		Print(-1)
	}
}

package main

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

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

	fmt.Fscan(in, &n);
	var first = make([]int64, n)
	for i := 0; i < n; i++{
		fmt.Fscan(in, &first[i])
	}
	var m int
	fmt.Fscan(in, &m)
	var second = make([]int64, m)
	for i := 0; i < m; i++{
		fmt.Fscan(in, &second[i])
	}
	var sumFirst = first[0]
	var sumSecond = second[0]
	for i := 1; i < n; i++ {
		sumFirst += first[i]
	}
	for i := 1; i < m; i++ {
		sumSecond += second[i]
	}
	if sumFirst != sumSecond {
		fmt.Println(-1)
		return
	}
	var firstIndx = 1
	var secondIndx = 1
	sumFirst = first[0]
	sumSecond = second[0]
	var answ = 0
	for {
		if sumFirst == sumSecond {
			answ++
			sumFirst = 0
			sumSecond = 0
			if firstIndx == n || secondIndx == m {
				break
			}
			sumFirst += first[firstIndx]
			sumSecond += second[secondIndx]
			firstIndx++
			secondIndx++
		} else {
			if sumFirst < sumSecond {
				sumFirst += first[firstIndx]
				firstIndx++
			} else {
				sumSecond += second[secondIndx]
				secondIndx++
			}
		}
	}

	fmt.Println(answ)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
//
func main(){
	in := bufio.NewReader(os.Stdin)
	var n int

	fmt.Fscan(in, &n);
	var first = make([]int64, n)
	for i := 0; i < n; i++{
		fmt.Fscan(in, &first[i])
	}
	var m int
	fmt.Fscan(in, &m)
	var second = make([]int64, m)
	for i := 0; i < m; i++{
		fmt.Fscan(in, &second[i])
	}
	var sumFirst = first[0]
	var sumSecond = second[0]
	for i := 1; i < n; i++ {
		sumFirst += first[i]
	}
	for i := 1; i < m; i++ {
		sumSecond += second[i]
	}
	if sumFirst != sumSecond {
		fmt.Println(-1)
		return
	}
	var firstIndx = 1
	var secondIndx = 1
	sumFirst = first[0]
	sumSecond = second[0]
	var answ = 0
	for {
		if sumFirst == sumSecond {
			answ++
			sumFirst = 0
			sumSecond = 0
			if firstIndx == n || secondIndx == m {
				break
			}
			sumFirst += first[firstIndx]
			sumSecond += second[secondIndx]
			firstIndx++
			secondIndx++
		} else {
			if sumFirst < sumSecond {
				sumFirst += first[firstIndx]
				firstIndx++
			} else {
				sumSecond += second[secondIndx]
				secondIndx++
			}
		}
	}

	fmt.Println(answ)
}

package main

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

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

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

func main() {
	defer writer.Flush()
	var n, m int
	scanf("%d\n", &n)
	arr1 := [311111]int64{}

	mp := make(map[int64]bool)
	for i := 0; i < n; i = i + 1 {
		scanf("%d", &arr1[i])
		if i > 0 {
			arr1[i] = arr1[i] + arr1[i-1]
		}
		mp[arr1[i]] = true
	}
	scanf("\n")
	scanf("%d\n", &m)
	arr2 := [311111]int64{}

	var ans = 0

	for i := 0; i < m; i = i + 1 {
		scanf("%d", &arr2[i])
		if i > 0 {
			arr2[i] = arr2[i] + arr2[i-1]
		}
		_, exists := mp[arr2[i]]
		if exists {
			ans = ans + 1
		}
	}
	//scanf("\n")
	//fmt.Print(n, m)

	if arr1[n-1] != arr2[m-1] {
		printf("%d\n", -1)
	} else {
		//fmt.Print(ans)
		printf("%d\n", ans)
	}

}

package main

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

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

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

func main() {
	defer writer.Flush()
	var n, m int
	scanf("%d\n", &n)
	arr1 := [311111]int64{}

	mp := make(map[int64]bool)
	for i := 0; i < n; i = i + 1 {
		scanf("%d", &arr1[i])
		if i > 0 {
			arr1[i] = arr1[i] + arr1[i-1]
		}
		mp[arr1[i]] = true
	}
	scanf("\n")
	scanf("%d\n", &m)
	arr2 := [311111]int64{}

	var ans = 0

	for i := 0; i < m; i = i + 1 {
		scanf("%d", &arr2[i])
		if i > 0 {
			arr2[i] = arr2[i] + arr2[i-1]
		}
		_, exists := mp[arr2[i]]
		if exists {
			ans = ans + 1
		}
	}
	//scanf("\n")
	fmt.Print(n, m)

	if arr1[n-1] != arr2[m-1] {
		printf("%d\n", -1)
	} else {
		//fmt.Print(ans)
		printf("%d\n", ans)
	}

}

package main

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

var n, m int
var a, b []int64
var sums map[int64]bool

func main() {
	buf := bufio.NewReader(os.Stdin)
	sums = make(map[int64]bool)
	fmt.Fscanf(buf, "%d\n", &n)
	s := int64(0)
	for i := 0; i < n; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		sums[s] = true
	}
	cnt := 0
	fmt.Fscanf(buf, "%d\n", &m)
	s = 0
	for i := 0; i < m; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		if sums[s] {
			cnt++
		}
	}
	if cnt == 0 {
		cnt = -1
	}
	fmt.Println(cnt)

}

package main

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

var n, m int
var a, b []int64
var sums map[int64]bool

func main() {
	buf := bufio.NewReader(os.Stdin)
	sums = make(map[int64]bool)
	fmt.Fscanf(buf, "%d\n", &n)
	s := int64(0)
	for i := 0; i < n; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		sums[s] = true
	}
	s1 := s
	cnt := 0
	fmt.Fscanf(buf, "\n%d\n", &m)
	s = 0
	for i := 0; i < m; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		if sums[s] {
			cnt++
		}
	}
	if cnt == 0 || !sums[s1] {
		cnt = -1
	}
	fmt.Println(cnt)

}

package main

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

var n, m int
var a, b []int64
var sums map[int64]bool

func main() {
	buf := bufio.NewReader(os.Stdin)
	sums = make(map[int64]bool)
	fmt.Fscanf(buf, "%d\n", &n)
	s := int64(0)
	for i := 0; i < n; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		sums[s] = true
	}
	cnt := 0
	fmt.Fscanf(buf, "\n%d\n", &m)
	s = 0
	for i := 0; i < m; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		if sums[s] {
			cnt++
		}
	}
	if cnt == 0 || !sums[s] {
		cnt = -1
	}
	fmt.Println(cnt)

}

package main

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

var n, m int
var a, b []int64
var sums map[int64]bool

func main() {
	buf := bufio.NewReader(os.Stdin)
	sums = make(map[int64]bool)
	fmt.Fscanf(buf, "%d\n", &n)
	s := int64(0)
	for i := 0; i < n; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		sums[s] = true
	}
	cnt := 0
	fmt.Fscanf(buf, "\n%d\n", &m)
	s = 0
	for i := 0; i < m; i++ {
		var tmp int64
		fmt.Fscanf(buf,"%d", &tmp)
		s += tmp
		if sums[s] {
			cnt++
		}
	}
	if cnt == 0 {
		cnt = -1
	}
	fmt.Println(cnt)

}

package main

import (
	"fmt"
)

func main(){
	var n int

	fmt.Scanf("%d", &n);
	var first = make([]int, n)
	for i := 0; i < n; i++{
		fmt.Scanf("%d", &first[i])
	}
	var m int
	fmt.Scanf("%d", &m)
	var second = make([]int, m)
	for i := 0; i < m; i++{
		fmt.Scanf("%d", &second[i])
	}
	var sumFirst = first[0]
	var sumSecond = second[0]
	for i := 1; i < n; i++ {
		sumFirst += first[i]
	}
	for i := 1; i < m; i++ {
		sumSecond += second[i]
	}
	if sumFirst != sumSecond {
		fmt.Println(-1)
		return
	}
	var firstIndx = 1
	var secondIndx = 1
	sumFirst = first[0]
	sumSecond = second[0]
	var answ = 0
	for {
		if sumFirst == sumSecond {
			answ++
			sumFirst = 0
			sumSecond = 0
			sumFirst += first[firstIndx]
			sumSecond += second[secondIndx]
			firstIndx++
			secondIndx++
			if firstIndx == n || secondIndx == m {
				answ++
				break
			}
		} else {
			if sumFirst < sumSecond {
				sumFirst += first[firstIndx]
				firstIndx++
			} else {
				sumSecond += second[secondIndx]
				secondIndx++
			}
		}
	}

	fmt.Println(answ)
}


One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (x1, y1), (x2, y2), ..., (xn, yn). Let's define neighbors for some fixed point from the given set (x, y):   point (x', y') is (x, y)'s right neighbor, if x' > x and y' = y  point (x', y') is (x, y)'s left neighbor, if x' < x and y' = y  point (x', y') is (x, y)'s lower neighbor, if x' = x and y' < y  point (x', y') is (x, y)'s upper neighbor, if x' = x and y' > y We'll consider point (x, y) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points.Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set.

The first input line contains the only integer n (1 ≤ n ≤ 200) — the number of points in the given set. Next n lines contain the coordinates of the points written as "x y" (without the quotes) (|x|, |y| ≤ 1000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different.

Print the only number — the number of supercentral points of the given set.

NoteIn the first sample the supercentral points are only points (1, 1) and (1, 2).In the second sample there is one supercental point — point (0, 0).

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	xs := make([]int, n)
	ys := make([]int, n)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		scanner.Scan()
		x, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		scanner.Scan()
		y, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		xs[i] = x
		ys[i] = y
	}

	cnt := 0
	for i := 0; i < n; i++ {
		if is_supercental(xs, ys, i, n) {
			cnt++
		}
	}
	fmt.Println(cnt)
}

func is_supercental(xs, ys []int, p, n int) bool {
	array := [4]bool{false, false, false, false} // Upper, Lower, Right, Left
	for i := 0; i < n; i++ {
		if i != p {
			if xs[i] == xs[p] {
				if ys[i] > ys[p] {
					array[0] = true
				} else if ys[i] < ys[p] {
					array[1] = true
				}
			}
			if ys[i] == ys[p] {
				if xs[i] > xs[p] {
					array[2] = true
				} else if xs[i] < xs[p] {
					array[3] = true
				}
			}
		}
	}
	if array[0] && array[1] && array[2] && array[3] {
		return true
	} else {
		return false
	}
}

package main

import "fmt"

func main() {
    var n int
    var x,y [205]int
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&x[i],&y[i])
    }
    ans := 0
    for i := 0; i < n; i++ {
        right, left, lower, upper := false, false, false, false
        for j := 0; j < n; j++ {
            if (x[j] > x[i]) && (y[j] == y[i]) { right = true }
            if (x[j] < x[i]) && (y[j] == y[i]) { left = true }
            if (x[j] == x[i]) && (y[j] < y[i]) { lower = true }
            if (x[j] == x[i]) && (y[j] > y[i]) { upper = true }
        }
        if left && right && lower && upper {
            ans++
        }
    }
    fmt.Println(ans)
}


So the Beautiful Regional Contest (BeRC) has come to an end! $$$n$$$ students took part in the contest. The final standings are already known: the participant in the $$$i$$$-th place solved $$$p_i$$$ problems. Since the participants are primarily sorted by the number of solved problems, then $$$p_1 \ge p_2 \ge \dots \ge p_n$$$.Help the jury distribute the gold, silver and bronze medals. Let their numbers be $$$g$$$, $$$s$$$ and $$$b$$$, respectively. Here is a list of requirements from the rules, which all must be satisfied:  for each of the three types of medals, at least one medal must be awarded (that is, $$$g>0$$$, $$$s>0$$$ and $$$b>0$$$);  the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, $$$g<s$$$ and $$$g<b$$$, but there are no requirements between $$$s$$$ and $$$b$$$);  each gold medalist must solve strictly more problems than any awarded with a silver medal;  each silver medalist must solve strictly more problems than any awarded a bronze medal;  each bronze medalist must solve strictly more problems than any participant not awarded a medal;  the total number of medalists $$$g+s+b$$$ should not exceed half of all participants (for example, if $$$n=21$$$, then you can award a maximum of $$$10$$$ participants, and if $$$n=26$$$, then you can award a maximum of $$$13$$$ participants). The jury wants to reward with medals the total maximal number participants (i.e. to maximize $$$g+s+b$$$) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains an integer $$$n$$$ ($$$1 \le n \le 4\cdot10^5$$$) — the number of BeRC participants. The second line of a test case contains integers $$$p_1, p_2, \dots, p_n$$$ ($$$0 \le p_i \le 10^6$$$), where $$$p_i$$$ is equal to the number of problems solved by the $$$i$$$-th participant from the final standings. The values $$$p_i$$$ are sorted in non-increasing order, i.e. $$$p_1 \ge p_2 \ge \dots \ge p_n$$$. The sum of $$$n$$$ over all test cases in the input does not exceed $$$4\cdot10^5$$$.

Print $$$t$$$ lines, the $$$j$$$-th line should contain the answer to the $$$j$$$-th test case. The answer consists of three non-negative integers $$$g, s, b$$$.   Print $$$g=s=b=0$$$ if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time.  Otherwise, print three positive numbers $$$g, s, b$$$ — the possible number of gold, silver and bronze medals, respectively. The sum of $$$g+s+b$$$ should be the maximum possible. If there are several answers, print any of them.

NoteIn the first test case, it is possible to reward $$$1$$$ gold, $$$2$$$ silver and $$$3$$$ bronze medals. In this case, the participant solved $$$5$$$ tasks will be rewarded with the gold medal, participants solved $$$4$$$ tasks will be rewarded with silver medals, participants solved $$$2$$$ or $$$3$$$ tasks will be rewarded with bronze medals. Participants solved exactly $$$1$$$ task won't be rewarded. It's easy to see, that in this case, all conditions are satisfied and it is possible to reward participants in this way. It is impossible to give more than $$$6$$$ medals because the number of medals should not exceed half of the number of participants. The answer $$$1$$$, $$$3$$$, $$$2$$$ is also correct in this test case.In the second and third test cases, it is impossible to reward medals, because at least one medal of each type should be given, but the number of medals should not exceed half of the number of participants.

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

const LEN = int(2e5)

func f(th int) {
	length := 0
	scanf("%d\n", &length)
	var l []int
	pre := -1
	cnt := 0
	for i := 0; i < length; i++ {
		t := 0
		scanf("%d", &t)
		if i == 0 {
			cnt = 1
			pre = t
		} else if t != pre {
			l = append(l, cnt)
			cnt = 1
			pre = t
		} else {
			cnt++
		}
	}
	scanf("\n")
	l = append(l, cnt)
	if length < 10 || len(l) < 4 {
		fmt.Printf("0 0 0\n")
		return
	}
	g := l[0]
	s := 0
	i := 1
	for i = 1; i < len(l); i++ {
		s += l[i]
		if s > g {
			break
		}
	}
	if s <= g {
		fmt.Printf("0 0 0\n")
		return
	}
	mx := length / 2 - g - s
	if mx <= g {
		fmt.Printf("0 0 0\n")
		return
	}
	b := 0
	for i++; i < len(l); i++ {
		if b + l[i] > mx {
			break
		}
		b += l[i]
	}
	if b <= g {
		fmt.Printf("0 0 0\n")
		return
	}
	fmt.Printf("%d %d %d\n", g, s, b)
}

func main() {
	defer writer.Flush()

	t := 0
	scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		f(i)
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

const LEN = int(2e5)

func f(th int) {
	length := 0
	scanf("%d\n", &length)
	var l []int
	pre := -1
	cnt := 0
	for i := 0; i < length; i++ {
		t := 0
		scanf("%d", &t)
		if i == 0 {
			cnt = 1
			pre = t
		} else if t != pre {
			l = append(l, cnt)
			cnt = 1
			pre = t
		} else {
			cnt++
		}
	}
	scanf("\n")
	l = append(l, cnt)
	if length < 10 || len(l) < 4 {
		printf("0 0 0\n")
		return
	}
	g := l[0]
	s := 0
	i := 1
	for i = 1; i < len(l); i++ {
		s += l[i]
		if s > g {
			break
		}
	}
	if s <= g {
		printf("0 0 0\n")
		return
	}
	mx := length / 2 - g - s
	if mx <= g {
		printf("0 0 0\n")
		return
	}
	b := 0
	for i++; i < len(l); i++ {
		if b + l[i] > mx {
			break
		}
		b += l[i]
	}
	if b <= g {
		printf("0 0 0\n")
		return
	}
	printf("%d %d %d\n", g, s, b)
}

func main() {
	defer writer.Flush()

	t := 0
	scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		f(i)
	}
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

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

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var t int
var n int
var P []int

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		n = ReadInt()
		P = ReadIntSlice(n)

		solve()
	}
}

func solve() {
	g, s, b := 0, 0, 0
	limit := n / 2

	_, counts := RunLengthEncoding(P)
	// 圧縮後に最低長さ3は必要
	if len(counts) < 3 {
		fmt.Println(0, 0, 0)
		return
	}

	// gを決める
	g += counts[0]

	// sを決める、伸ばす
	idx := 1
	for idx < len(counts) {
		// gを超えたらbreak
		if s > g {
			break
		}

		s += counts[idx]
		idx++
	}

	// bを決める、伸ばす
	for idx < len(counts) {
		// gを超えたらbreak
		if b > g {
			break
		}

		b += counts[idx]
		idx++
	}

	if g == 0 || s == 0 || b == 0 {
		fmt.Println(0, 0, 0)
		return
	}
	if s <= g || b <= g {
		fmt.Println(0, 0, 0)
		return
	}
	sum := g + s + b
	if sum > limit {
		fmt.Println(0, 0, 0)
		return
	}

	// bをできるだけ伸ばす
	for idx < len(counts) {
		// 足した結果がlimitを超えるなら足さないでbreak
		if sum+counts[idx] > limit {
			break
		}

		b += counts[idx]
		sum += counts[idx]
		idx++
	}

	fmt.Println(g, s, b)
}

// RunLengthEncoding returns encoded slice of an input.
func RunLengthEncoding(S []int) ([]int, []int) {
	runes := []int{}
	lengths := []int{}

	l := 0
	for i := 0; i < len(S); i++ {
		// 1文字目の場合保持
		if i == 0 {
			l = 1
			continue
		}

		if S[i-1] == S[i] {
			// 直前の文字と一致していればインクリメント
			l++
		} else {
			// 不一致のタイミングで追加し、長さをリセットする
			runes = append(runes, S[i-1])
			lengths = append(lengths, l)
			l = 1
		}
	}
	runes = append(runes, S[len(S)-1])
	lengths = append(lengths, l)

	return runes, lengths
}

// RunLengthDecoding decodes RLE results.
func RunLengthDecoding(S []int, L []int) []int {
	if len(S) != len(L) {
		panic("S, L are not RunLengthEncoding results")
	}

	res := []int{}

	for i := 0; i < len(S); i++ {
		for j := 0; j < L[i]; j++ {
			res = append(res, S[i])
		}
	}

	return res
}

/*
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
*/

/*******************************************************************/

package main

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

var reader *bufio.Reader=bufio.NewReader(os.Stdin)
var writer *bufio.Writer=bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}){fmt.Fprintf(writer,f,a...)}
func scanf(f string,a...interface{}){fmt.Fscanf(reader,f,a...)}

var p,cnt,pre [400010]int

func main(){
	defer writer.Flush()
	p[0]=-1
	var T int
	scanf("%d\n",&T)
	for cas:=1;cas<=T;cas++{
		var n int
		scanf("%d\n",&n)
		for i:=1;i<=n;i++{if i<n{scanf("%d",&p[i])}else{scanf("%d\n",&p[i])}}
		m:=0
		for i:=1;i<=n;i++{
			if p[i]!=p[i-1] {
				m++
				cnt[m]=1
			}else{cnt[m]++}
		}
		for i:=1;i<=m;i++{pre[i]=pre[i-1]+cnt[i]}
		mk:=0
		for mk<m && pre[mk+1]<=n/2 {mk++}
		if mk<3 {printf("0 0 0\n");continue}
		flg:=2
		fd:=false
		g,s,b:=0,0,0
		for i:=1;i<m;i++{
			tg:=pre[i]
			for flg<mk && pre[flg]-tg<=tg{flg++}
			if pre[flg]>n/2 || pre[flg]-tg<=tg || flg==mk || pre[mk]-pre[flg]<=tg{break}
			fd=true
			g=tg;s=pre[flg]-tg;b=pre[mk]-pre[flg]
		}
		if fd{printf("%d %d %d\n",g,s,b)}else{printf("0 0 0\n")}
	}
}


You are given an integer $$$n$$$. In $$$1$$$ move, you can do one of the following actions:  erase any digit of the number (it's acceptable that the number before the operation has exactly one digit and after the operation, it is "empty");  add one digit to the right. The actions may be performed in any order any number of times.Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number $$$301$$$ the digit $$$3$$$, the result is the number $$$01$$$ (not $$$1$$$).You need to perform the minimum number of actions to make the number any power of $$$2$$$ (i.e. there's an integer $$$k$$$ ($$$k \ge 0$$$) such that the resulting number is equal to $$$2^k$$$). The resulting number must not have leading zeroes.E.g. consider $$$n=1052$$$. The answer is equal to $$$2$$$. First, let's add to the right one digit $$$4$$$ (the result will be $$$10524$$$). Then let's erase the digit $$$5$$$, so the result will be $$$1024$$$ which is a power of $$$2$$$.E.g. consider $$$n=8888$$$. The answer is equal to $$$3$$$. Let's erase any of the digits $$$8$$$ three times. The result will be $$$8$$$ which is a power of $$$2$$$.

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

For each test case, output in a separate line one integer $$$m$$$ — the minimum number of moves to transform the number into any power of $$$2$$$.

NoteThe answer for the first test case was considered above.The answer for the second test case was considered above.In the third test case, it's enough to add to the right the digit $$$4$$$ — the number $$$6$$$ will turn into $$$64$$$.In the fourth test case, let's add to the right the digit $$$8$$$ and then erase $$$7$$$ and $$$5$$$ — the taken number will turn into $$$8$$$.The numbers of the fifth and the sixth test cases are already powers of two so there's no need to make any move.In the seventh test case, you can delete first of all the digit $$$3$$$ (the result is $$$01$$$) and then the digit $$$0$$$ (the result is $$$1$$$).

package main

import (
	"fmt"
	"math"
)

func main() {
	start := 2
	res := int64(2)
	count := 0
	arr := []int64{}
	final := math.Floor(math.Log2(math.MaxInt64))
	for float64(count) < final {
		res = int64(math.Pow(float64(start), float64((count))))
		arr = append(arr, res)
		count++
	}
	var n int
	fmt.Scan(&n)
	len := len(arr)
	for i := 0; i < n; i++ {
		var number int
		fmt.Scan(&number)
		min := int64(math.MaxInt64)
		for j := 0; j < len; j++ {
			min = getMin(handle(int64(number), arr[j]), min)
		}
		fmt.Println(min)
	}
}

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

func handle(num, powerNum int64) int64 {
	numArr := []int64{}
	for num != 0 {
		numArr = append(numArr, num%10)
		num = num / 10
	}
	len0 := len(numArr) - 1
	for i, j := 0, len0; i < j; i, j = i+1, j-1 {
		numArr[i], numArr[j] = numArr[j], numArr[i]
	}

	powerNumArr := []int64{}
	for powerNum != 0 {
		powerNumArr = append(powerNumArr, powerNum%10)
		powerNum = powerNum / 10
	}
	len1 := len(powerNumArr) - 1
	for i, j := 0, len1; i < j; i, j = i+1, j-1 {
		powerNumArr[i], powerNumArr[j] = powerNumArr[j], powerNumArr[i]
	}
	count := int64(0)
	len2 := len(powerNumArr)
	for i := 0; i < len2; i++ {
		if len(numArr)-1 < i {
			numArr = append(numArr, powerNumArr[i])
			count++
		}
		if powerNumArr[i] != numArr[i] {
			numArr = append(numArr[:i], numArr[i+1:]...)
			count++
			i--
		}
	}
	count = count + int64(len(numArr)-len(powerNumArr))
	return count
}

package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	var t int
	fmt.Scan(&t)

	var strArr [][]string
	for i := int64(1); i < math.MaxInt64/2; i *= 2 {
		str := fmt.Sprint(i)
		strArr = append(strArr, strings.Split(str, ""))
	}

	for i := 1; i <= t; i++ {
		var n string
		fmt.Scan(&n)
		minn := math.MaxInt32
		for _, v := range strArr {
			minn = min(minn, calculate(strings.Split(n, ""), v))
		}
		fmt.Println(minn)
	}
}

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

func calculate(source, target []string) int {
	ptr := 0
	for _, v := range source {
		if ptr < len(target) && v == target[ptr] {
			ptr++
		}
	}
	return len(source) + len(target) - 2*ptr
}

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

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

		res := 10
		for i := 0; i < 64; i++ {
			s := strconv.FormatInt(int64(1)<<i, 10)
			k := 0
			for j := 0; j < len(n); j++ {
				if k < len(s) && s[k] == n[j] {
					k++
				}
			}
			res = min(res, len(n) + len(s) - (k<<1))
		}
		Fprintln(out, res)
	}
}

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

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

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

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

		res := 10
		for i := 0; i < 64; i++ {
			s := strconv.FormatInt(int64(1)<<i, 10)
			k := 0
			for j := 0; j < len(n); j++ {
				if k < len(s) && s[k] == n[j] {
					k++
				}
			}
			res = min(res, len(n) - k + len(s) - k)
		}
		Fprintln(out, res)
	}
}

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

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

package main

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

func CF1063A(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

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

		res := int(1e9)
		for i := 0; i < 64; i++ {
			k := 0
			s := strconv.FormatInt(int64(1)<<i, 10)
			for j := 0; j < len(n); j++ {
				if k < len(s) && s[k] == n[j] {
					k++
				}
			}
			res = min(res, len(s) + len(n) - 2 * k)
		}
		Fprintln(out, res)
	}
}

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

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

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

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

		res := int(1e9)
		for i := 0; i < 64; i++ {
			s := strconv.FormatInt(int64(1)<<i, 10)
			k := 0
			for j := 0; j < len(n); j++ {
				if k < len(s) && s[k] == n[j] {
					k++
				}
			}
			res = int(math.Min(float64(res), float64(len(n) + len(s) - 2 * k)))
		}
		Fprintln(out, res)
	}
}

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

package main

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

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

	m := make([]string, 64)
	p := int64(1)
	for i := 0; i < 64; i++ {
		m[i] = strconv.FormatInt(p, 10)
		p *= 2
	}

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

		match := func(s string) int {
			min := len(s) + 1

			for _, pw := range m {
				var tp, sp, tot int

				for sp < len(s) && tp < len(pw) {
					if s[sp] == pw[tp] {
						tot++
						tp++
					}
					sp++
				}

				if len(s)-tot+len(pw)-tot < min {
					min = len(s) - tot + len(pw) - tot
				}
			}
			return min
		}

		fmt.Fprintln(out, match(s))
	}
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		var n int64
		fmt.Fscan(r, &n)

		a := solve(n)

		fmt.Println(a)
	}
}

func solve(n int64) int {

	var ans int = 1e6

	for i := 0; i <= 63; i++ {
		var t int64 = 1 << uint(i)
		pow := strconv.FormatInt(t, 10)
		given := strconv.FormatInt(n, 10)

		//10121252 -- given
		//128 -- pow

		local := len(given) + len(pow)
		if local < ans {
			ans = local
		}

		for k := 0; k < len(given); k++ {
			cur := given[k]

			if pow[0] == cur {
				ops := k

				i, j := k, 0
				i++
				j++

				for ;i < len(given) && j < len(pow); {
					if given[i] == pow[j] {
						i++
						j++
						continue
					}
					i++
					ops++
					continue
				}

				if j <= len(pow) - 1{
					ops += len(pow) - j
				}
				if i <= len(given) - 1 {
					ops += len(given) - i
				}

				if ops < ans {
					ans = ops
				}
			}
		}
	}

	return ans
}

func editDistDP(str1, str2 string) int {
// Create a table to store results of subproblems
	dp := make([][]int, len(str1)+1)

	for i := 0; i < len(str1)+1; i++ {
		dp[i] = make([]int, len(str2)+1)
	}

	m := len(str1)
	n := len(str2)

	// Fill d[][] in bottom up manner
	for i := 0; i <= m; i++ {
		for j := 0; j <= n; j++ {
			// If first string is empty, only option is
			// to insert all characters of second string
			if i == 0 {
				dp[i][j] = j
			} else if j == 0 {
				dp[i][j] = i
			} else if str1[i-1] == str2[j-1] {
				dp[i][j] = dp[i-1][j-1]

			} else {
				dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j]) // Replace
			}
		}
	}

	return dp[m][n]
}

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

package main

import "fmt"

var data []string

func solve(str, pow string) int {
	s, p, match := 0, 0, 0
	for ; s < len(str) && p < len(pow); s++ {
		if str[s] == pow[p] {
			p++
			match++
		}
	}
	return len(str) - match + len(pow) - match
}

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

func d() {
	var str string
	fmt.Scanf("%s\n", &str)

	times := len(str) + 1
	for _, pow := range data {
		times = min(solve(str, pow), times)
	}

	fmt.Println(times)
}

func main() {
	var i int64
	for i = 1; i <= 2e18; i *= 2 {
		data = append(data, fmt.Sprintf("%d", i))
	}

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

	for ; test > 0; test-- {
		d()
	}
}

package main

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

var in = NewIn()
var out = NewOut()

func main() {
	defer out.flush()

	tests()
	// solve()
}

/* --- Solution D --- */
func solve() {
	num := in.next()

	bin := []string{}

	for k := 0; (1 << k) < int64(2e18); k++ {
		bin = append(bin, strconv.FormatInt(1<<k, 10))
	}

	var dist func(string, string) int
	dist = func(v, w string) (d int) {
		i, j := 0, 0
		d += len(v) + len(w)
		for i < len(v) && j < len(w) {
			if v[i] == w[j] {
				d -= 2
				j++
			}
			i++
		}
		return
	}

	sort.Slice(bin, func(i, j int) bool { return dist(num, bin[i]) < dist(num, bin[j]) })

	out.println(dist(num, bin[0]))
}

func tests() {
	tt := in.nextInt()
	for ; tt > 0; tt-- {
		solve()
	}
}

/* --- I/O --- */
type In struct {
	scanner *bufio.Scanner
}

func NewIn() *In {
	in := In{bufio.NewScanner(os.Stdin)}
	in.scanner.Split(bufio.ScanWords)
	return &in
}

func (in *In) nextInt() int {
	in.scanner.Scan()
	i, _ := strconv.Atoi(in.scanner.Text())
	return i
}

func (in *In) nextLong() int64 {
	in.scanner.Scan()
	i, _ := strconv.ParseInt(in.scanner.Text(), 10, 64)
	return i
}

func (in *In) next() string {
	in.scanner.Scan()
	return in.scanner.Text()
}

type Out struct {
	writer *bufio.Writer
}

func NewOut() *Out {
	out := Out{bufio.NewWriter(os.Stdout)}
	return &out
}

func (out *Out) println(a ...interface{}) {
	fmt.Fprintln(out.writer, a...)
}

func (out *Out) printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

func (out *Out) print(a ...interface{}) {
	fmt.Fprint(out.writer, a...)
}

func (out *Out) flush() {
	out.writer.Flush()
}

package main

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

var (
	out = bufio.NewWriter(os.Stdout)
	in  = bufio.NewReader(os.Stdin)
	arr []string
)

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}

func init() {
	arr = make([]string, 61)
	for i := 0; i <= 60; i++ {
		arr[i] = strconv.FormatInt(int64(math.Pow(2, float64(i))), 10)
	}
	//fmt.Println(arr)
}

func solve() {
	var givenNumber string
	fmt.Fscan(in, &givenNumber)
	var ans int64 = math.MaxInt64
	for i := 0; i < len(arr); i++ {
		currentNumber := arr[i]
		j := 0
		k := 0
		var noMatched int64 = 0
		for j < len(currentNumber) && k < len(givenNumber) {
			if currentNumber[j] == givenNumber[k] {
				j++
				k++
			} else {
				k++
				noMatched++
			}
		}
		var tempAns int64
		tempAns = int64(len(currentNumber) - j)
		tempAns += noMatched
		if k < len(givenNumber) {
			tempAns += int64(len(givenNumber) - k)
		}
		ans = min(tempAns, ans)
	}
	fmt.Println(ans)
}

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

func lcsMethod(s1 string, s2 string, s1Index int, s2Index int, tempAns int) int {
	if s1Index >= len(s1) || s2Index >= len(s2) {
		return tempAns
	}
	if s1[s1Index] == s2[s2Index] {
		tempAns = lcsMethod(s1, s2, s2Index+1, s2Index+1, tempAns+1)
	} else {
		tempAns = int(math.Max(float64(tempAns), math.Max(float64(lcsMethod(s1, s2, s1Index+1, s2Index, tempAns)), float64(lcsMethod(s1, s2, s1Index, s2Index+1, tempAns)))))
	}
	return tempAns
}

func printArray(arr []int) {
	for i := 1; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

type runeSort []rune

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

package main

import (
	"bufio"
	"fmt"
	"math/big"
	"os"
	"strconv"
)

func main() {
	defer wr.Flush()
	t := scanInt()
	for i := 0; i < t; i++ {
		fmt.Fprintln(wr, solve())
	}
}

func solve() int {
	x := itoa(scanInt())
	ret := 1<<31 - 1
	for _, y := range powOfTwo {
		need := len(x) + len(y) - 2*f(x, y)
		ret = min(ret, need)
	}
	return ret
}

var powOfTwo []string

func g(dp [][]int, p, q string, i, j int) int {
	if i == len(p) || j == len(q) {
		return 0
	} else if p[i] == q[j] {
		dp[i][j] = g(dp, p, q, i+1, j+1) + 1
		return dp[i][j]
	}
	dp[i][j] = g(dp, p, q, i+1, j)
	return dp[i][j]
}

func f(p, q string) int {
	dp := make([][]int, len(p))
	for i := range dp {
		dp[i] = make([]int, len(q))
	}
	return g(dp, p, q, 0, 0)
}

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

func init() {
	x := big.NewInt(1)
	y := big.NewInt(2)
	for i := 0; ; i++ {
		s := x.String()
		powOfTwo = append(powOfTwo, s)
		if len(s) > 21 {
			break
		}
		x = x.Mul(x, y)
	}
}

func itoa(x int) string {
	return strconv.Itoa(x)
}

var (
	sc *bufio.Scanner
	wr *bufio.Writer
)

func init() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	wr = bufio.NewWriter(os.Stdout)
}

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

package main

import (
	"bufio"
	"fmt"
	"os"
)
func min(x,y int)int{
	if x<y{
		return x
	}
	return y
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out:= os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for c:=1;c<=t;c++{
		var n int64
		fmt.Fscan(in,&n)
		var pow [100]int64
		pow[0]=1
		for i:=1;i<=62;i++{
			pow[i]=pow[i-1]*2
		}
		ret:=9999
		for i:=0;i<=62;i++{
			d := n
			lst:=-1
			ans:=0
			d=n
			sizen:=0
			for d >0{
				sizen++
				d/=10
			}
			sizet:=0
			d = pow[i]
			for d >0{
				sizet++
				d/=10
			}
			p:= [305]int64{}
			tempn := [305]int64{}
			d = n
			cnt := sizen
			for d>0{
				tempn[cnt]= d%10
				cnt--
				d/=10
			}
			d = pow[i]
			cnt = sizet
			for d>0{
				p[cnt]=d%10
				cnt--
				d/=10
			}
			for k:=1;k<=sizet;k++ {
				index := -1
				for j := 1; j <= sizen; j++ {
					if p[k] == tempn[j] && j > lst {
						index = j
						ans++
						break
					}
				}
				if index == -1 {
					break
				}
				lst = index
			}
			ret = min(ret,sizen-ans+sizet-ans)
		}
		fmt.Fprintln(out,ret)
	}
}

package main

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

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func calc(x, y int64) int64 {
	s, t := strconv.FormatInt(x, 10), strconv.FormatInt(y, 10)
	p := 0
	for q := 0; p < len(s) && q < len(t); q++ {
		if t[q] == s[p] {
			p++
		}
	}
	return int64(len(t) + len(s) - 2 * p);
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	var p2 []int64
	var tmp, ans int64
	tmp = 1
	p2 = append(p2, tmp)
	for tmp < 1e18 {
		tmp <<= 1
		p2 = append(p2, tmp)
	}
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int64
		fmt.Fscan(in, &n)
		ans = 10
		for _, j := range p2 {
			 ans = min(ans, calc(j, n))
		}
		fmt.Fprintln(out, ans)
	}
}

/******************************************************************************

                            Online Go Lang Compiler.
                Code, Compile, Run and Debug Go Lang program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

package main

import "fmt"
import "bufio"
import "os"
//import "sort"

const M int = 62

var s[M] string

func to_string(x int64) string{
    var ans string
    for ; x > 0; x /= 10{
        ans += string('0' + x % 10)
    }
    return ans
}

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

func dist(x, y string) int{
    j := 1
    for i := 1; i <= len(x); i++ {
        for ; j <= len(y) && y[len(y) - j] != x[len(x) - i]; j++ {
        }
        if (j > len(y)) {
            return i - 1
        }
        j++
    }
    return min(len(x), len(y))
}

func main() {
    in := bufio.NewReader(os.Stdin)
    var c int64 = 1;
    for i := 1; i < M; i++{
        s[i] = to_string(c)
        c *= 2
    }
    var t int
    fmt.Fscan(in, &t)
    for ; t > 0; t-- {
        var n int64
        fmt.Fscan(in, &n)
        ans := M
        n1 := to_string(n)
        for i := 1; i < M; i++{
            g := len(s[i]) + len(n1) - (dist(s[i], n1) * 2)
            if ans > g {
                ans = g
            }
        }
        fmt.Println(ans)
    }

}

package main

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

func main() {
	var t int64
	r := bufio.NewReader(os.Stdin)
	fmt.Fscan(r, &t)
	for i := int64(0); i < t; i++ {
		res := int64(10)
		var n string
		fmt.Fscan(r, &n)
		for j := 0; j < 63; j++ {
			str := strconv.FormatInt(int64(1)<<j, 10)
			l := int64(0)
			for k := 0; k < len(n); k++ {
				if n[k] == str[l]  {
					l++
					if l>=int64(len(str)){
						break
					}
				}
			}
			res = min(res, int64(len(n))+int64(len(str))-2*l)
		}
		fmt.Println(res)
	}
}

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

	return a
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1560D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T int
	var s []byte
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &s)
		ans := 99
		for k := 0; k < 61; k++ {
			t := strconv.FormatInt(1<<k, 10)
			j := 0
			for _, x := range s {
				if j < len(t) && t[j] == x {
					j++
				}
			}
			ans = min(ans, len(s)+len(t)-j*2)
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

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 t int
	var n int32
	fmt.Fscan(r, &t)
	for ti := 0; ti < t; ti++ {
		fmt.Fscan(r, &n)
		w1 := do(n)
		fmt.Fprint(w, w1)
		fmt.Fprintln(w)
	}
}

func do(n int32) int {
	s := fmt.Sprintf("%d", n)
	r := -1
	for _, t := range a {
		d := dist(t, s)
		if r == -1 || r > d {
			r = d
		}
	}
	return r
}

func dist(a, b string) int {
	i := 0
	j := 0
	r := 0
	for {
		for j < len(b) && a[i] != b[j] {
			j++
			r++
		}
		if j == len(b) {
			r += len(a) - i
			break
		}
		i++
		j++
		if i == len(a) {
			r += len(b) - j
			break
		}
	}
	return r
}

var a []string

func init() {
	i := uint64(1)
	for k := 1; k <= 64; k++ {
		a = append(a, fmt.Sprintf("%d", i))
		i *= 2
	}
}

package main

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

func longestPrefix(l, r string) int {
	n := len(l)
	m := len(r)
	f := make([][]int, n+1)

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

	res := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if l[i] == r[j] {
				if j == 0 {
					f[i][j] = 1
					if j+1 > res {
						res = j + 1
					}
					continue
				}

				for k := 0; k < i; k++ {
					if f[k][j-1] != 0 {
						f[i][j] = 1
						if j+1 > res {
							res = j + 1
						}
					}
				}
			}
		}
	}

	return res
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T, n int

	fmt.Fscanf(r, "%d\n", &T)

	for t := 0; t < T; t++ {
		fmt.Fscanf(r, "%d\n", &n)

		res := 256
		l := fmt.Sprintf("%d", n)
		for i := 0; i < 62; i++ {
			r := fmt.Sprintf("%d", int64(1)<<i)

			p := longestPrefix(l, r)

			x := len(r) - p + len(l) - p
			//	fmt.Println(l, r, p, x)

			if x < res {
				res = x
			}
		}
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	start := 2
	res := int64(2)
	count := 0
	arr := []int64{}
	for res <= 100000000000 {
		res = int64(math.Pow(float64(start), float64((count))))
		arr = append(arr, res)
		count++
	}
	var n int
	fmt.Scan(&n)
	len := len(arr)
	// fmt.Print64ln(arr)
	for i := 0; i < n; i++ {
		var number int
		fmt.Scan(&number)
		min := int64(math.MaxInt64)
		for j := 0; j < len; j++ {
			min = getMin(handle(int64(number), arr[j]), min)
		}
		fmt.Println(min)
	}
}

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

func handle(num, powerNum int64) int64 {
	numArr := []int64{}
	for num != 0 {
		numArr = append(numArr, num%10)
		num = num / 10
	}
	for i, j := 0, len(numArr)-1; i < j; i, j = i+1, j-1 {
		numArr[i], numArr[j] = numArr[j], numArr[i]
	}

	powerNumArr := []int64{}
	for powerNum != 0 {
		powerNumArr = append(powerNumArr, powerNum%10)
		powerNum = powerNum / 10
	}
	for i, j := 0, len(powerNumArr)-1; i < j; i, j = i+1, j-1 {
		powerNumArr[i], powerNumArr[j] = powerNumArr[j], powerNumArr[i]
	}
	count := int64(0)
	for i := 0; i < len(powerNumArr); i++ {
		if len(numArr)-1 < i {
			numArr = append(numArr, powerNumArr[i])
			count++
		}
		if powerNumArr[i] != numArr[i] {
			numArr = append(numArr[:i], numArr[i+1:]...)
			count++
			i--
		}
	}
	count = count + int64(len(numArr)-len(powerNumArr))
	return count
}

package main

import (
	"fmt"
	"math"
)

func main() {
	start := 2
	res := int64(2)
	count := 0
	arr := []int64{}
	for res <= 10000000000 {
		res = int64(math.Pow(float64(start), float64((count))))
		arr = append(arr, res)
		count++
	}
	var n int
	fmt.Scan(&n)
	len := len(arr)
	// fmt.Print64ln(arr)
	for i := 0; i < n; i++ {
		var number int
		fmt.Scan(&number)
		min := int64(math.MaxInt64)
		for j := 0; j < len; j++ {
			min = getMin(handle(int64(number), arr[j]), min)
		}
		fmt.Println(min)
	}
}

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

func handle(num, powerNum int64) int64 {
	numArr := []int64{}
	for num != 0 {
		numArr = append(numArr, num%10)
		num = num / 10
	}
	for i, j := 0, len(numArr)-1; i < j; i, j = i+1, j-1 {
		numArr[i], numArr[j] = numArr[j], numArr[i]
	}

	powerNumArr := []int64{}
	for powerNum != 0 {
		powerNumArr = append(powerNumArr, powerNum%10)
		powerNum = powerNum / 10
	}
	for i, j := 0, len(powerNumArr)-1; i < j; i, j = i+1, j-1 {
		powerNumArr[i], powerNumArr[j] = powerNumArr[j], powerNumArr[i]
	}
	count := int64(0)
	for i := 0; i < len(powerNumArr); i++ {
		if len(numArr)-1 < i {
			numArr = append(numArr, powerNumArr[i])
			count++
		}
		if powerNumArr[i] != numArr[i] {
			numArr = append(numArr[:i], numArr[i+1:]...)
			count++
			i--
		}
	}
	count = count + int64(len(numArr)-len(powerNumArr))
	return count
}

package main

import (
	"fmt"
	"math"
)

func main() {
	start := 2
	res := 2
	count := 0
	arr := []int{}
	for res <= 1000000000 {
		res = int(math.Pow(float64(start), float64((count))))
		arr = append(arr, res)
		count++
	}
	var n int
	fmt.Scan(&n)
	len := len(arr)
	for i := 0; i < n; i++ {
		var number int
		fmt.Scan(&number)
		min := int(math.MaxInt32)
		for j := 0; j < len; j++ {
			min = getMin(handle(number, arr[j]), min)
		}
		fmt.Println(min)
	}
}

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

func handle(num, powerNum int) int {
	numArr := []int{}
	for num != 0 {
		numArr = append(numArr, num%10)
		num = num / 10
	}
	for i, j := 0, len(numArr)-1; i < j; i, j = i+1, j-1 {
		numArr[i], numArr[j] = numArr[j], numArr[i]
	}

	powerNumArr := []int{}
	for powerNum != 0 {
		powerNumArr = append(powerNumArr, powerNum%10)
		powerNum = powerNum / 10
	}
	for i, j := 0, len(powerNumArr)-1; i < j; i, j = i+1, j-1 {
		powerNumArr[i], powerNumArr[j] = powerNumArr[j], powerNumArr[i]
	}
	count := 0
	for i := 0; i < len(powerNumArr); i++ {
		if len(numArr)-1 < i {
			numArr = append(numArr, powerNumArr[i])
			count++
		}
		if powerNumArr[i] != numArr[i] {
			numArr = append(numArr[:i], numArr[i+1:]...)
			count++
			i--
		}
	}
	count = count + len(numArr) - len(powerNumArr)
	return count
}

package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	var t int
	fmt.Scan(&t)

	var strArr [][]string
	for i := int64(1); i <= int64(1e10); i *= 2 {
		str := fmt.Sprint(i)
		strArr = append(strArr, strings.Split(str, ""))
	}

	for i := 1; i <= t; i++ {
		var n string
		fmt.Scan(&n)
		minn := math.MaxInt32
		for _, v := range strArr {
			minn = min(minn, calculate(strings.Split(n, ""), v))
		}
		fmt.Println(minn)
	}
}

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

func calculate(source, target []string) int {
	ptr := 0
	for _, v := range source {
		if ptr < len(target) && v == target[ptr] {
			ptr++
		}
	}
	return len(source) + len(target) - 2*ptr
}

package main

import (
	"fmt"
	"math"
	"strings"
)

func main() {
	var t int
	fmt.Scan(&t)

	var strArr [][]string
	for i := int64(1); i <= int64(math.MaxInt32); i *= 2 {
		str := fmt.Sprint(i)
		strArr = append(strArr, strings.Split(str, ""))
	}

	for i := 1; i <= t; i++ {
		var n string
		fmt.Scan(&n)
		minn := math.MaxInt32
		for _, v := range strArr {
			minn = min(minn, calculate(strings.Split(n, ""), v))
		}
		fmt.Println(minn)
	}
}

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

func calculate(source, target []string) int {
	ptr := 0
	for _, v := range source {
		if ptr < len(target) && v == target[ptr] {
			ptr++
		}
	}
	return len(source) + len(target) - 2*ptr
}

package main

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

func CF1063A(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

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

		res := int(1e9)
		for i := 0; i < 64; i++ {
			k := 0
			s := strconv.FormatInt(int64(i)<<i, 10)
			for j := 0; j < len(n); j++ {
				if k < len(s) && s[k] == n[j] {
					k++
				}
			}
			res = min(res, len(s) + len(n) - 2 * k)
		}
		Fprintln(out, res)
	}
}

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

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

package main

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

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

	m := make([]string, 40)
	p := int64(1)
	for i := 0; i < 40; i++ {
		m[i] = strconv.FormatInt(p, 10)
		p *= 2
	}

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

		match := func(s string) int {
			min := len(s) + 1

			for _, pw := range m {
				var tp, sp, tot int

				for sp < len(s) && tp < len(pw) {
					if s[sp] == pw[tp] {
						tot++
						tp++
					}
					sp++
				}

				if len(s)-tot+len(pw)-tot < min {
					min = len(s) - tot + len(pw) - tot
				}
			}
			return min
		}

		fmt.Fprintln(out, match(s))
	}
}

package main

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

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

	m := make([]string, 40)
	p := int64(1)
	for i := 0; i < 40; i++ {
		m[i] = strconv.FormatInt(p, 10)
		p *= 2
	}

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

		match := func(s string) int {
			min := len(s) + 1

			for _, pw := range m {
				a, b := s, pw
				if len(b) > len(a) {
					a, b = b, a
				}

				var tot int
				var x int
				for i := 0; i < len(a); i++ {
					if x < len(b) && a[i] == b[x] {
						x++
					} else {
						tot++
					}
				}

				if tot+len(b)-x < min {
					min = tot + len(b) - x
				}
			}
			return min
		}

		fmt.Fprintln(out, match(s))
	}
}

package main

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

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

	m := make([]string, 40)
	p := 1
	for i := 0; i < 40; i++ {
		m[i] = strconv.Itoa(p)
		p *= 2
	}

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

		match := func(s string) int {
			min := len(s) + 1

			for _, pw := range m {
				a, b := s, pw
				if len(b) > len(a) {
					a, b = b, a
				}

				var tot int
				var x int
				for i := 0; i < len(a); i++ {
					if x < len(b) && a[i] == b[x] {
						x++
					} else {
						tot++
					}
				}

				if tot+len(b)-x < min {
					min = tot + len(b) - x
				}
			}
			return min
		}

		fmt.Fprintln(out, match(s))
	}
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		var n int64
		fmt.Fscan(r, &n)

		a := solve(n)

		fmt.Println(a)
	}
}

func solve(n int64) int {

	var ans int = 1e6

	for i := 0; i <= 64; i++ {
		var t int64 = 1 << uint(i)
		pow := strconv.FormatInt(t, 10)
		given := strconv.FormatInt(n, 10)

		//10121252 -- given
		//128 -- pow

		local := len(given) + len(pow)
		if local < ans {
			ans = local
		}

		for k := 0; k < len(given); k++ {
			cur := given[k]

			if pow[0] == cur {
				ops := k

				i, j := k, 0
				i++
				j++

				for ;i < len(given) && j < len(pow); {
					if given[i] == pow[j] {
						i++
						j++
						continue
					}
					i++
					ops++
					continue
				}

				if j <= len(pow) - 1{
					ops += len(pow) - j
				}
				if i <= len(given) - 1 {
					ops += len(given) - i
				}

				if ops < ans {
					ans = ops
				}
			}
		}
	}

	return ans
}

func editDistDP(str1, str2 string) int {
// Create a table to store results of subproblems
	dp := make([][]int, len(str1)+1)

	for i := 0; i < len(str1)+1; i++ {
		dp[i] = make([]int, len(str2)+1)
	}

	m := len(str1)
	n := len(str2)

	// Fill d[][] in bottom up manner
	for i := 0; i <= m; i++ {
		for j := 0; j <= n; j++ {
			// If first string is empty, only option is
			// to insert all characters of second string
			if i == 0 {
				dp[i][j] = j
			} else if j == 0 {
				dp[i][j] = i
			} else if str1[i-1] == str2[j-1] {
				dp[i][j] = dp[i-1][j-1]

			} else {
				dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j]) // Replace
			}
		}
	}

	return dp[m][n]
}

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

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		var n int64
		fmt.Fscan(r, &n)

		a := solve(n)

		fmt.Println(a)
	}
}

func solve(n int64) int {

	var ans int = 1e6

	for i := 0; i <= 50; i++ {
		var t int64 = 1 << uint(i)
		pow := strconv.FormatInt(t, 10)
		given := strconv.FormatInt(n, 10)

		//10121252 -- given
		//128 -- pow

		local := len(given) + len(pow)
		if local < ans {
			ans = local
		}

		for k := 0; k < len(given); k++ {
			cur := given[k]

			if pow[0] == cur {
				ops := k

				i, j := k, 0
				i++
				j++

				for ;i < len(given) && j < len(pow); {
					if given[i] == pow[j] {
						i++
						j++
						continue
					}
					i++
					ops++
					continue
				}

				if j <= len(pow) - 1{
					ops += len(pow) - j
				}
				if i <= len(given) - 1 {
					ops += len(given) - i
				}

				if ops < ans {
					ans = ops
				}
			}
		}
	}

	return ans
}

func editDistDP(str1, str2 string) int {
// Create a table to store results of subproblems
	dp := make([][]int, len(str1)+1)

	for i := 0; i < len(str1)+1; i++ {
		dp[i] = make([]int, len(str2)+1)
	}

	m := len(str1)
	n := len(str2)

	// Fill d[][] in bottom up manner
	for i := 0; i <= m; i++ {
		for j := 0; j <= n; j++ {
			// If first string is empty, only option is
			// to insert all characters of second string
			if i == 0 {
				dp[i][j] = j
			} else if j == 0 {
				dp[i][j] = i
			} else if str1[i-1] == str2[j-1] {
				dp[i][j] = dp[i-1][j-1]

			} else {
				dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j]) // Replace
			}
		}
	}

	return dp[m][n]
}

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

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(r, &n)

		a := solve(n)

		fmt.Println(a)
	}
}

func solve(n int) int {

	var ans int = 1e6

	for i := 0; i <= 50; i++ {
		t := 1 << uint(i)
		pow := strconv.Itoa(t)
		given := strconv.Itoa(n)

		//10121252 -- given
		//128 -- pow

		local := len(given) + len(pow)
		if local < ans {
			ans = local
		}

		for k := 0; k < len(given); k++ {
			cur := given[k]

			if pow[0] == cur {
				ops := k

				i, j := k, 0
				i++
				j++

				for ;i < len(given) && j < len(pow); {
					if given[i] == pow[j] {
						i++
						j++
						continue
					}
					i++
					ops++
					continue
				}

				if j <= len(pow) - 1{
					ops += len(pow) - j
				}
				if i <= len(given) - 1 {
					ops += len(given) - i
				}

				if ops < ans {
					ans = ops
				}
			}
		}
	}

	return ans
}

func editDistDP(str1, str2 string) int {
// Create a table to store results of subproblems
	dp := make([][]int, len(str1)+1)

	for i := 0; i < len(str1)+1; i++ {
		dp[i] = make([]int, len(str2)+1)
	}

	m := len(str1)
	n := len(str2)

	// Fill d[][] in bottom up manner
	for i := 0; i <= m; i++ {
		for j := 0; j <= n; j++ {
			// If first string is empty, only option is
			// to insert all characters of second string
			if i == 0 {
				dp[i][j] = j
			} else if j == 0 {
				dp[i][j] = i
			} else if str1[i-1] == str2[j-1] {
				dp[i][j] = dp[i-1][j-1]

			} else {
				dp[i][j] = 1 + min(dp[i][j-1], dp[i-1][j]) // Replace
			}
		}
	}

	return dp[m][n]
}

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

package main

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

var in = NewIn()
var out = NewOut()

func main() {
	defer out.flush()

	tests()
	// solve()
}

/* --- Solution D --- */
func solve() {
	num := in.next()

	bin := []string{}

	for k := int64(0); (1 << k) < int64(2e18); k++ {
		bin = append(bin, strconv.Itoa(1<<k))
	}

	var dist func(string, string) int
	dist = func(v, w string) (d int) {
		i, j := 0, 0
		d += len(v) + len(w)
		for i < len(v) && j < len(w) {
			if v[i] == w[j] {
				d -= 2
				j++
			}
			i++
		}
		return
	}

	sort.Slice(bin, func(i, j int) bool { return dist(num, bin[i]) < dist(num, bin[j]) })

	out.println(dist(num, bin[0]))
}

func tests() {
	tt := in.nextInt()
	for ; tt > 0; tt-- {
		solve()
	}
}

/* --- I/O --- */
type In struct {
	scanner *bufio.Scanner
}

func NewIn() *In {
	in := In{bufio.NewScanner(os.Stdin)}
	in.scanner.Split(bufio.ScanWords)
	return &in
}

func (in *In) nextInt() int {
	in.scanner.Scan()
	i, _ := strconv.Atoi(in.scanner.Text())
	return i
}

func (in *In) nextLong() int64 {
	in.scanner.Scan()
	i, _ := strconv.ParseInt(in.scanner.Text(), 10, 64)
	return i
}

func (in *In) next() string {
	in.scanner.Scan()
	return in.scanner.Text()
}

type Out struct {
	writer *bufio.Writer
}

func NewOut() *Out {
	out := Out{bufio.NewWriter(os.Stdout)}
	return &out
}

func (out *Out) println(a ...interface{}) {
	fmt.Fprintln(out.writer, a...)
}

func (out *Out) printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

func (out *Out) print(a ...interface{}) {
	fmt.Fprint(out.writer, a...)
}

func (out *Out) flush() {
	out.writer.Flush()
}

package main

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

var in = NewIn()
var out = NewOut()

func main() {
	defer out.flush()

	tests()
	// solve()
}

/* --- Solution D --- */
func solve() {
	num := in.next()

	bin := []string{}

	for k := 0; (1 << k) < int64(2e18); k++ {
		bin = append(bin, strconv.Itoa(1<<k))
	}

	var dist func(string, string) int
	dist = func(v, w string) (d int) {
		i, j := 0, 0
		d += len(v) + len(w)
		for i < len(v) && j < len(w) {
			if v[i] == w[j] {
				d -= 2
				j++
			}
			i++
		}
		return
	}

	sort.Slice(bin, func(i, j int) bool { return dist(num, bin[i]) < dist(num, bin[j]) })

	out.println(dist(num, bin[0]))
}

func tests() {
	tt := in.nextInt()
	for ; tt > 0; tt-- {
		solve()
	}
}

/* --- I/O --- */
type In struct {
	scanner *bufio.Scanner
}

func NewIn() *In {
	in := In{bufio.NewScanner(os.Stdin)}
	in.scanner.Split(bufio.ScanWords)
	return &in
}

func (in *In) nextInt() int {
	in.scanner.Scan()
	i, _ := strconv.Atoi(in.scanner.Text())
	return i
}

func (in *In) nextLong() int64 {
	in.scanner.Scan()
	i, _ := strconv.ParseInt(in.scanner.Text(), 10, 64)
	return i
}

func (in *In) next() string {
	in.scanner.Scan()
	return in.scanner.Text()
}

type Out struct {
	writer *bufio.Writer
}

func NewOut() *Out {
	out := Out{bufio.NewWriter(os.Stdout)}
	return &out
}

func (out *Out) println(a ...interface{}) {
	fmt.Fprintln(out.writer, a...)
}

func (out *Out) printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

func (out *Out) print(a ...interface{}) {
	fmt.Fprint(out.writer, a...)
}

func (out *Out) flush() {
	out.writer.Flush()
}

package main

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

var (
	out = bufio.NewWriter(os.Stdout)
	in  = bufio.NewReader(os.Stdin)
	arr []string
)

func main() {
	var test int
	fmt.Fscan(in, &test)
	for i := 0; i < test; i++ {
		solve()
	}

}

func init() {
	arr = make([]string, 61)
	for i := 0; i <= 60; i++ {
		arr[i] = strconv.Itoa(int(math.Pow(2, float64(i))))
	}
}

func solve() {
	var givenNumber string
	fmt.Fscan(in, &givenNumber)
	var ans int64 = math.MaxInt64
	for i := 0; i < len(arr); i++ {
		currentNumber := arr[i]
		j := 0
		k := 0
		noMatched := 0
		for j < len(currentNumber) && k < len(givenNumber) {
			if currentNumber[j] == givenNumber[k] {
				j++
				k++
			} else {
				k++
				noMatched++
			}
		}
		tempAns := len(currentNumber) - j
		tempAns += noMatched
		if k < len(givenNumber) {
			tempAns += len(givenNumber) - k
		}
		ans = int64(math.Min(float64(tempAns), float64(ans)))
	}
	fmt.Println(ans)
}

func lcsMethod(s1 string, s2 string, s1Index int, s2Index int, tempAns int) int {
	if s1Index >= len(s1) || s2Index >= len(s2) {
		return tempAns
	}
	if s1[s1Index] == s2[s2Index] {
		tempAns = lcsMethod(s1, s2, s2Index+1, s2Index+1, tempAns+1)
	} else {
		tempAns = int(math.Max(float64(tempAns), math.Max(float64(lcsMethod(s1, s2, s1Index+1, s2Index, tempAns)), float64(lcsMethod(s1, s2, s1Index, s2Index+1, tempAns)))))
	}
	return tempAns
}

func printArray(arr []int) {
	for i := 1; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

type runeSort []rune

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

package main

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

func main() {
	defer wr.Flush()
	t := scanInt()
	for i := 0; i < t; i++ {
		fmt.Fprintln(wr, solve())
	}
}

func solve() int {
	x := itoa(scanInt())
	ret := 1<<31 - 1
	for _, y := range powOfTwo {
		need := len(x) + len(y) - 2*f(x, y)
		ret = min(ret, need)
	}
	return ret
}

var powOfTwo []string

func g(dp [][]int, p, q string, i, j int) int {
	if i == len(p) || j == len(q) {
		return 0
	} else if p[i] == q[j] {
		dp[i][j] = g(dp, p, q, i+1, j+1) + 1
		return dp[i][j]
	}
	dp[i][j] = g(dp, p, q, i+1, j)
	return dp[i][j]
}

func f(p, q string) int {
	dp := make([][]int, len(p))
	for i := range dp {
		dp[i] = make([]int, len(q))
	}
	return g(dp, p, q, 0, 0)
}

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

func init() {
	for i := 0; i < 63; i++ {
		s := itoa(1 << i)
		powOfTwo = append(powOfTwo, s)
	}
}

func itoa(x int) string {
	return strconv.Itoa(x)
}

var (
	sc *bufio.Scanner
	wr *bufio.Writer
)

func init() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	wr = bufio.NewWriter(os.Stdout)
}

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

package main

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

func min(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func calc(x, y int64) int64 {
	s, t := string(x), string(y)
	p := 0
	for q := 0; p < len(s) && q < len(t); q++ {
		if t[q] == s[p] {
			p++
		}
	}
	return int64(len(t) + len(s) - 2 * p);
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	var p2 []int64
	var tmp, ans int64
	tmp = 1
	p2 = append(p2, tmp)
	for tmp < 1e18 {
		tmp <<= 1
		p2 = append(p2, tmp)
	}
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n int64
		fmt.Fscan(in, &n)
		ans = 10
		for _, j := range p2 {
			 ans = min(ans, calc(j, n))
		}
		fmt.Fprintln(out, ans)
	}
}

/******************************************************************************

                            Online Go Lang Compiler.
                Code, Compile, Run and Debug Go Lang program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

package main

import "fmt"
import "bufio"
import "os"
//import "sort"

const M int = 50

var s[M] string

func to_string(x int) string{
    var ans string
    for ; x > 0; x /= 10{
        ans += string('0' + x % 10)
    }
    return ans
}

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

func dist(x, y string) int{
    j := 1
    for i := 1; i <= len(x); i++ {
        for ; j <= len(y) && y[len(y) - j] != x[len(x) - i]; j++ {
        }
        if (j > len(y)) {
            return i - 1
        }
        j++
    }
    return min(len(x), len(y))
}

func main() {
    in := bufio.NewReader(os.Stdin)
    c := 1;
    for i := 1; i < M; i++{
        s[i] = to_string(c)
        c *= 2
    }
    var t int
    fmt.Fscan(in, &t)
    for ; t > 0; t-- {
        var n int
        fmt.Fscan(in, &n)
        ans := M * 2
        n1 := to_string(n)
        for i := 1; i < M; i++{
            g := len(s[i]) + len(n1) - (dist(s[i], n1) * 2)
            if ans > g {
                ans = g
            }
        }
        fmt.Println(ans)
    }

}