You are given three integers $$$a$$$, $$$b$$$, and $$$d$$$. Your task is to find any integer $$$x$$$ which satisfies all of the following conditions, or determine that no such integers exist:  $$$0 \le x \lt 2^{60}$$$;  $$$a|x$$$ is divisible by $$$d$$$;  $$$b|x$$$ is divisible by $$$d$$$. Here, $$$|$$$ denotes the bitwise OR operation.

Each test contains multiple test cases. The first line of input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.  Each test case consists of one line, containing three integers $$$a$$$, $$$b$$$, and $$$d$$$ ($$$1 \le a,b,d \lt 2^{30}$$$).

For each test case print one integer. If there exists an integer $$$x$$$ which satisfies all of the conditions from the statement, print $$$x$$$. Otherwise, print $$$-1$$$. If there are multiple solutions, you may print any of them.

NoteIn the first test case, $$$x=18$$$ is one of the possible solutions, since $$$39|18=55$$$ and $$$12|18=30$$$, both of which are multiples of $$$d=5$$$.In the second test case, $$$x=14$$$ is one of the possible solutions, since $$$8|14=6|14=14$$$, which is a multiple of $$$d=14$$$.In the third and fourth test cases, we can show that there are no solutions.

package main

import (
  "fmt"
  "math/bits"
)

func main() {
  var T int
  fmt.Scan(&T);
  for t := 0; t < T; t++ {
    var a, b, d uint64
    fmt.Scan(&a, &b, &d);
    x := bits.TrailingZeros64(a);
    y := bits.TrailingZeros64(b);
    k := bits.TrailingZeros64(d);
    if x < k || y < k {
      fmt.Println(-1);
    } else {
      var ans uint64
      for l := k; l < 30; l++ {
        if (ans >> l) & 1 == 0 {
          ans += uint64(d << (l - k));
        }
      }
      fmt.Println(ans)
    }
  }
}

package main

import (
  "fmt"
  "math/bits"
)

func main() {
  var T int
  fmt.Scan(&T);
  for t := 0; t < T; t++ {
    var a, b, d uint64
    fmt.Scan(&a, &b, &d);
    x := bits.TrailingZeros64(a);
    y := bits.TrailingZeros64(b);
    k := bits.TrailingZeros64(d);
    if x < k || y < k {
      fmt.Println(-1);
    } else {
      var ans uint64
      for l := k; l < 30; l++ {
        if (ans >> l) & 1 == 0 {
          ans += uint64(d << l);
        }
      }
      fmt.Println(ans)
    }
  }
}


Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by $$$2$$$ but the number itself is not divisible by $$$2$$$. For example, $$$13$$$, $$$1227$$$, $$$185217$$$ are ebne numbers, while $$$12$$$, $$$2$$$, $$$177013$$$, $$$265918$$$ are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification.You are given a non-negative integer $$$s$$$, consisting of $$$n$$$ digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between $$$0$$$ (do not delete any digits at all) and $$$n-1$$$.For example, if you are given $$$s=$$$222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 $$$\rightarrow$$$ 2237344218521717191. The sum of digits of 2237344218521717191 is equal to $$$70$$$ and is divisible by $$$2$$$, but number itself is not divisible by $$$2$$$: it means that the resulting number is ebne.Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3000$$$)  — the number of digits in the original number. The second line of each test case contains a non-negative integer number $$$s$$$, consisting of $$$n$$$ digits. It is guaranteed that $$$s$$$ does not contain leading zeros and the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$.

For each test case given in the input print the answer in the following format:  If it is impossible to create an ebne number, print "-1" (without quotes); Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits.

NoteIn the first test case of the example, $$$1227$$$ is already an ebne number (as $$$1 + 2 + 2 + 7 = 12$$$, $$$12$$$ is divisible by $$$2$$$, while in the same time, $$$1227$$$ is not divisible by $$$2$$$) so we don't need to delete any digits. Answers such as $$$127$$$ and $$$17$$$ will also be accepted.In the second test case of the example, it is clearly impossible to create an ebne number from the given number.In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, $$$1$$$ digit such as $$$17703$$$, $$$77013$$$ or $$$17013$$$. Answers such as $$$1701$$$ or $$$770$$$ will not be accepted as they are not ebne numbers. Answer $$$013$$$ will not be accepted as it contains leading zeroes.Explanation: $$$1 + 7 + 7 + 0 + 3 = 18$$$. As $$$18$$$ is divisible by $$$2$$$ while $$$17703$$$ is not divisible by $$$2$$$, we can see that $$$17703$$$ is an ebne number. Same with $$$77013$$$ and $$$17013$$$; $$$1 + 7 + 0 + 1 = 9$$$. Because $$$9$$$ is not divisible by $$$2$$$, $$$1701$$$ is not an ebne number; $$$7 + 7 + 0 = 14$$$. This time, $$$14$$$ is divisible by $$$2$$$ but $$$770$$$ is also divisible by $$$2$$$, therefore, $$$770$$$ is not an ebne number.In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 $$$\rightarrow$$$ 22237320442418521717191 (delete the last digit).

package main

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

//•*¨*•.¸¸♪I/O•*¨*•.¸¸♪

var (
	sc = bufio.NewScanner(os.Stdin)
	wr = bufio.NewWriter(os.Stdout)
)

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

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

func scanFloat64() float64 {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return float64(a)
}

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ { res[i] = scanInt() }
	return res
}

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

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 100000000)

	t := scanInt()

	for i := 0; i < t; i++ {
		n := scanInt()
		s := scanText()
		ans := []rune{}
		for j := 0; j < n; j++ {
			num, _ := strconv.Atoi(string(s[j]))
			if num%2 != 1 {
				continue
			}

			ans = append(ans, rune(s[j]))
		}

		if len(ans)%2 == 1 {
			ans = ans[1:]
		}

		if len(ans) == 0 {
			fmt.Println(-1)
			continue
		}

		fmt.Println(string(ans))

	}
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var s string
		fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
		fmt.Fprintf(writer, "%s\n", makeEbne(s))
	}
}

func makeEbne(s string) string {
	strWr := bytes.NewBufferString("")
	for _, ch := range s {
		d := int(ch) - '0'
		if d%2 != 0 {
			fmt.Fprintf(strWr, "%d", d)
		}
		if len(strWr.String()) == 2 {
			return strWr.String()
		}
	}
	return "-1"
}

package main

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

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

	solve := func() (ans string) {
		var n int
		var s []byte
		Fscan(in, &n, &s)
		for _, b := range s {

			if (b-'0')&1 == 1 {
				ans += string(b)
				if len(ans) == 2 {
					return
				}
			}
		}

		return "-1"
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		_ans := solve()
		Fprintln(out, _ans)
	}

}

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

package main

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

func main() {
	var T int
	var n int
	var s string
	var ans string
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(in, &T)
	for _case := 0; _case < T; _case++ {
		fmt.Fscan(in, &n,  &s)
		ans = ""
		for _, b := range s {
			if (b-'0')&1 == 1 {
				ans += string(b)
				if len(ans) == 2 {
					break
				}
			}
		}
		if len(ans) != 2 {
			ans = "-1"
		}
		fmt.Fprintln(out, ans)
	}
}

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

	var t int
	fmt.Fscan(r, &t)
	ans := make([]int, t)
	for i := 0; i < t; i++ {
		var n, j, a int
		var s string
		fmt.Fscan(r, &n, &s)
		for _, c := range s {
			if int(c - '0')%2 != 0 {
				a *= 10
				a += int(c - '0')
				j++
				if j == 2 {
					ans[i] = a
					break
				}
			}
		}
	}
	for _, i := range ans {
		if i == 0 {
			fmt.Fprintln(w, "-1")
		} else {
			fmt.Fprintln(w, i)
		}
	}
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var s string
		fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
		fmt.Fprintf(writer, "%s\n", makeEbne(s))
	}
}

func makeEbne(s string) string {
	n := len(s)

	if n <= 1 {
		return "-1"
	}

	ds := make([]int, 0, n)
	for _, d := range s {
		ds = append(ds, int(d)-'0')
	}

	if (n > 0) && (ds[n-1]%2) == 0 {
		n--
		ds = ds[:n]
	}

	if n == 0 {
		return "-1"
	}

	sum := 0
	for _, d := range ds {
		sum += d
	}

	skip := -1
	if sum%2 != 0 {
		for i, d := range ds {
			if (d % 2) != 0 {
				skip = i
				break
			}
		}
		if skip == -1 {
			return "-1"
		}
	}

	ret := ""
	strWr := bytes.NewBufferString(ret)
	for i, d := range ds {
		if i == skip {
			continue
		}
		fmt.Fprintf(strWr, "%d", d)
	}

	if len(s) == 0 {
		return "-1"
	}

	return strWr.String()
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var s string
		fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
		fmt.Fprintf(writer, "%s\n", makeEbne(s))
	}
}

func makeEbne(s string) string {
	n := len(s)

	ds := make([]int, 0, n)
	for _, d := range s {
		ds = append(ds, int(d)-'0')
	}

	for (n > 0) && (ds[n-1]%2 == 0) {
		n--
		ds = ds[:n]
	}

	sum := 0
	for _, d := range ds {
		sum += d
	}

	skip := -1
	if sum%2 != 0 {
		for i, d := range ds {
			if (d % 2) != 0 {
				skip = i
				break
			}
		}
		if skip == -1 || skip == n-1 {
			return "-1"
		}
	}

	ret := ""
	strWr := bytes.NewBufferString(ret)
	for i, d := range ds {
		if i == skip {
			continue
		}
		fmt.Fprintf(strWr, "%d", d)
	}

	if len(strWr.String()) == 0 {
		return "-1"
	}

	return strWr.String()
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var s string
		fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
		fmt.Fprintf(writer, "%s\n", makeEbne(s))
	}
}

func makeEbne(s string) string {
	n := len(s)

	ds := make([]int, 0, n)
	for _, d := range s {
		ds = append(ds, int(d)-'0')
	}

	for (n > 0) && (ds[n-1]%2 == 0) {
		n--
		ds = ds[:n]
	}

	sum := 0
	for _, d := range ds {
		sum += d
	}

	skip := -1
	if sum%2 != 0 {
		for i, d := range ds {
			if (d % 2) != 0 {
				skip = i
				break
			}
		}
		if skip == -1 {
			return "-1"
		}
	}

	ret := ""
	strWr := bytes.NewBufferString(ret)
	for i, d := range ds {
		if i == skip {
			continue
		}
		fmt.Fprintf(strWr, "%d", d)
	}

	if len(strWr.String()) == 0 {
		return "-1"
	}

	return strWr.String()
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n int
		var s string
		fmt.Fscanf(reader, "%d\n%s\n", &n, &s)
		fmt.Fprintf(writer, "%s\n", makeEbne(s))
	}
}

func makeEbne(s string) string {
	n := len(s)

	ds := make([]int, 0, n)
	for _, d := range s {
		ds = append(ds, int(d)-'0')
	}

	if (n > 0) && (ds[n-1]%2) == 0 {
		n--
		ds = ds[:n]
	}

	if n == 0 {
		return "-1"
	}

	sum := 0
	for _, d := range ds {
		sum += d
	}

	skip := -1
	if sum%2 != 0 {
		for i, d := range ds {
			if (d % 2) != 0 {
				skip = i
				break
			}
		}
		if skip == -1 {
			return "-1"
		}
	}

	ret := ""
	strWr := bytes.NewBufferString(ret)
	for i, d := range ds {
		if i == skip {
			continue
		}
		fmt.Fprintf(strWr, "%d", d)
	}

	if len(strWr.String()) == 0 {
		return "-1"
	}

	return strWr.String()
}

package main

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

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

	solve := func() (ans string) {
		var n int
		var s []byte
		Fscan(in, &n, &s)
		for _, b := range s {

			if (b-'0') &1==1 {
				return string(b)
			}
		}

		return "-1"
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		_ans := solve()
		Fprintln(out, _ans)
	}

}

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


Levko loves permutations very much. A permutation of length n is a sequence of distinct positive integers, each is at most n.Let’s assume that value gcd(a, b) shows the greatest common divisor of numbers a and b. Levko assumes that element pi of permutation p1, p2, ... , pn is good if gcd(i, pi) > 1. Levko considers a permutation beautiful, if it has exactly k good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them.

The single line contains two integers n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ n).

In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them.

NoteIn the first sample elements 4 and 3 are good because gcd(2, 4) = 2 > 1 and gcd(3, 3) = 3 > 1. Elements 2 and 1 are not good because gcd(1, 2) = 1 and gcd(4, 1) = 1. As there are exactly 2 good elements, the permutation is beautiful.The second sample has no beautiful permutations.

//361B
package main
import "fmt"

func main() {
	var nn, kk int
	var x[100005]int
	fmt.Scan(&nn, &kk)
	if kk==nn {
		fmt.Println("-1")
	} else {
		x[1]=nn-kk
		for i:=2; i<=nn-kk; i++ {
			x[i]=i-1
		}
		for i:=nn-kk+1; i<=nn; i++ {
			x[i]=i
		}
		fmt.Print(x[1])
		for i:=2; i<=nn; i++ {
			fmt.Print(" ", x[i])
		}
		fmt.Println()
	}
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var n, k int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscanln(input, &n, &k)

	if n == k {
		fmt.Println(-1)
		return
	}

	vp := n - k - 1
	fmt.Print(vp + 1)

	for i := 2; i <= n; i++ {
		if vp > 0 {
			fmt.Print(" ", i - 1)
			vp--
		} else {
			fmt.Print(" ", i)
		}
	}

}

// 361B-ahalim,dikai,eric
package main

import (
	"fmt"
)

func main() {
	var n,k,i int
	fmt.Scan(&n,&k)
	if k == n {
		fmt.Println("-1")
	}else{
		for i=1;i<n-k;i++{
			fmt.Print(i+1," ")
		}
		fmt.Print("1 ")

		for i=n-k+1;i<=n;i++{
			fmt.Print(i," ")
		}
		fmt.Println()
	}
}

// 361B
package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	if n == k {
		fmt.Print(-1)
		return
	}
	fmt.Printf("%d ", n-k)
	for i := 1; i <= n; i++ {
		if i != n-k {
			fmt.Printf("%d ", i)
		}
	}
}

// 361B-mic
package main

import (
	"fmt"
)

func main() {
	var A [100000]int
	var n, k int
	fmt.Scan(&n, &k)
	for i := 0; i < n; i++ {
		A[i] = i + 1
	}

	for i := 1; i < n; i++ {
		if k > 0 {
			k--
			continue
		}
		if k == 0 {
			if i+1 == n {
				A[0], A[i] = A[i], A[0]
				break
			}
			A[i], A[i+1] = A[i+1], A[i]
			i++
		}
	}
	if k == 1 {
		fmt.Print("-1 ")
	} else {
		for i := 0; i < n; i++ {
			fmt.Print(A[i], " ")
		}
	}
	fmt.Println()
	return
}

// http://acm.timus.ru/problem.aspx?space=1&num=1741
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

func main () {
	var n, k int

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscanln(input, &n, &k)

	if k == 0 || n == k {
		fmt.Println(-1)
		return
	}

	vp := n - k - 1
	fmt.Print(vp + 1)

	for i := 2; i <= n; i++ {
		if vp > 0 {
			fmt.Print(" ", i - 1)
			vp--
		} else {
			fmt.Print(" ", i)
		}
	}

}

// 361B-ahalim,dikai,eric
package main

import (
	"fmt"
)

func main() {
	var n,k,i int
	fmt.Scan(&n,&k)
	if k == n {
		fmt.Println("-1")
	}else{
		for i=1;i<n-k;i++{
			fmt.Print(i+1," ")
			fmt.Print("1 ")
		}
		for i=(n-k)+1;i<=n;i++{
			fmt.Print(i," ")
		}
		fmt.Println()

	}
}


A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so we don't care.There is an $$$n\times m$$$ grid. Initially, you may place some north magnets and some south magnets into the cells. You are allowed to place as many magnets as you like, even multiple in the same cell.An operation is performed as follows. Choose a north magnet and a south magnet to activate. If they are in the same row or the same column and they occupy different cells, then the north magnet moves one unit closer to the south magnet. Otherwise, if they occupy the same cell or do not share a row or column, then nothing changes. Note that the south magnets are immovable.Each cell of the grid is colored black or white. Let's consider ways to place magnets in the cells so that the following conditions are met.  There is at least one south magnet in every row and every column.  If a cell is colored black, then it is possible for a north magnet to occupy this cell after some sequence of operations from the initial placement.  If a cell is colored white, then it is impossible for a north magnet to occupy this cell after some sequence of operations from the initial placement. Determine if it is possible to place magnets such that these conditions are met. If it is possible, find the minimum number of north magnets required (there are no requirements on the number of south magnets).

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n,m\le 1000$$$)  — the number of rows and the number of columns, respectively. The next $$$n$$$ lines describe the coloring. The $$$i$$$-th of these lines contains a string of length $$$m$$$, where the $$$j$$$-th character denotes the color of the cell in row $$$i$$$ and column $$$j$$$. The characters "#" and "." represent black and white, respectively. It is guaranteed, that the string will not contain any other characters.

Output a single integer, the minimum possible number of north magnets required. If there is no placement of magnets that satisfies all conditions, print a single integer $$$-1$$$.

NoteIn the first test, here is an example placement of magnets:  In the second test, we can show that no required placement of magnets exists. Here are three example placements that fail to meet the requirements. The first example violates rule $$$3$$$ since we can move the north magnet down onto a white square. The second example violates rule $$$2$$$ since we cannot move the north magnet to the bottom-left black square by any sequence of operations. The third example violates rule $$$1$$$ since there is no south magnet in the first column.  In the third test, here is an example placement of magnets. We can show that there is no required placement of magnets with fewer north magnets.  In the fourth test, we can show that no required placement of magnets exists. Here are two example placements that fail to meet the requirements. The first example violates rule $$$1$$$ since there is no south magnet in the first row. The second example violates rules $$$1$$$ and $$$3$$$ since there is no south magnet in the second row and we can move the north magnet up one unit onto a white square.  In the fifth test, we can put the south magnet in each cell and no north magnets. Because there are no black cells, it will be a correct placement.

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

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

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

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n, m := NextInt(), NextInt()
	s := make([][]byte, n)
	was := make([][]bool, n)

	rowGood, colGood := make([]bool, n), make([]bool, m)
	badRows, badCols := n, m

	for i := 0; i < n; i++ {
		s[i] = NextBytes()
		was[i] = make([]bool, m)
		for j := 0; j < m; j++ {
			if s[i][j] == '#' {
				if !rowGood[i] {
					badRows--
				}
				rowGood[i] = true
				if !colGood[j] {
					badCols--
				}
				colGood[j] = true
			}
		}
	}

	if badCols > 0 && badRows == 0 || badCols == 0 && badRows > 0 {
		fmt.Fprintln(out, -1)
		return
	}

	for i := 0; i < n; i++ {
		if !rowGood[i] {
			continue
		}
		start, end := -1, -1
		cnt := 0
		for j := 0; j < m; j++ {
			if s[i][j] == '#' {
				if start == -1 {
					start = j
				}
				end = j
				cnt++
			}
		}
		if start == -1 || end-start+1 != cnt {
			fmt.Fprintln(out, -1)
			return
		}
	}

	for i := 0; i < m; i++ {
		if !colGood[i] {
			continue
		}
		start, end := -1, -1
		cnt := 0
		for j := 0; j < n; j++ {
			if s[j][i] == '#' {
				if start == -1 {
					start = j
				}
				end = j
				cnt++
			}
		}
		if start == -1 || end-start+1 != cnt {
			fmt.Fprintln(out, -1)
			return
		}
	}

	steps := [4][2]int{
		{0, -1},
		{-1, 0},
		{0, 1},
		{1, 0},
	}

	var dfs func(i, j int)
	dfs = func(i, j int) {
		was[i][j] = true
		for k := 0; k < 4; k++ {
			di, dj := steps[k][0], steps[k][1]
			ni, nj := i+di, j+dj
			if ni < 0 || ni == n || nj < 0 || nj == m {
				continue
			}
			if !was[ni][nj] && s[ni][nj] == '#' {
				dfs(ni, nj)
			}
		}
	}

	res := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if !was[i][j] && s[i][j] == '#' {
				dfs(i, j)
				res++
			}
		}
	}
	fmt.Fprintln(out, res)

}

package main

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

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

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

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

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

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

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

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

	return i
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, m := readTwoNums(reader)

	grid := make([]string, n)
	for i := 0; i < n; i++ {
		grid[i], _ = reader.ReadString('\n')
		grid[i] = grid[i][:m]
	}

	fmt.Println(solve(n, m, grid))
}

var dd = []int{-1, 0, 1, 0, -1}

func solve(n, m int, grid []string) int {
	col := make([]bool, m)

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' {
				// black
				if i > 0 && grid[i-1][j] == '.' {
					// white
					if col[j] {
						// has another black cell on top of white
						return -1
					}
				}
				col[j] = true
			}
		}
	}

	row := make([]bool, n)

	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if grid[i][j] == '#' {
				if j > 0 && grid[i][j-1] == '.' {
					if row[i] {
						return -1
					}
				}
				row[i] = true
			}
		}
	}

	var c1, c2 int

	for j := 0; j < m; j++ {
		if col[j] {
			c1++
		}
	}

	for i := 0; i < n; i++ {
		if row[i] {
			c2++
		}
	}

	if c1 > 0 && c1 < m && c2 == n {
		return -1
	}

	if c2 > 0 && c2 < n && c1 == m {
		return -1
	}

	// safe

	vis := make([][]bool, n)
	for i := 0; i < n; i++ {
		vis[i] = make([]bool, m)
	}

	var dfs func(x, y int)

	dfs = func(x, y int) {
		for k := 0; k < 4; k++ {
			u, v := x+dd[k], y+dd[k+1]
			if u < 0 || u >= n || v < 0 || v >= m {
				continue
			}
			if grid[u][v] == '#' && !vis[u][v] {
				vis[u][v] = true
				dfs(u, v)
			}
		}
	}

	var cnt int

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' && !vis[i][j] {
				cnt++
				vis[i][j] = true
				dfs(i, j)
			}
		}
	}

	return cnt
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

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

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

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n, m := NextInt(), NextInt()
	s := make([][]byte, n)
	was := make([][]bool, n)

	minrow, mincol, maxrow, maxcol := n, m, -1, -1

	for i := 0; i < n; i++ {
		s[i] = NextBytes()
		was[i] = make([]bool, m)
		for j := 0; j < m; j++ {
			if s[i][j] == '#' {
				minrow = min(minrow, i)
				mincol = min(mincol, j)
				maxrow = max(maxrow, i)
				maxcol = max(maxcol, j)
			}
		}
	}

	if minrow == n {
		fmt.Fprintln(out, 0)
		return
	}

	for i := minrow; i <= maxrow; i++ {
		s[i] = s[i][mincol : maxcol+1]
	}
	s = s[minrow : maxrow+1]
	newm := maxcol - mincol + 1
	newn := maxrow - minrow + 1

	if newm != m && newn == n || newm == m && newn != n {
		fmt.Fprintln(out, -1)
		return
	}
	n, m = newn, newm

	// stderr(mincol, maxcol)
	// stderr(n, m)
	// for i := 0; i < n; i++ {
	// 	stderr(string(s[i]))
	// }

	for i := 0; i < n; i++ {
		start, end := -1, -1
		cnt := 0
		for j := 0; j < m; j++ {
			if s[i][j] == '#' {
				if start == -1 {
					start = j
				}
				end = j
				cnt++
			}
		}
		if start == -1 || end-start+1 != cnt {
			fmt.Fprintln(out, -1)
			return
		}
	}

	for i := 0; i < m; i++ {
		start, end := -1, -1
		cnt := 0
		for j := 0; j < n; j++ {
			if s[j][i] == '#' {
				if start == -1 {
					start = j
				}
				end = j
				cnt++
			}
		}
		if start == -1 || end-start+1 != cnt {
			fmt.Fprintln(out, -1)
			return
		}
	}

	steps := [4][2]int{
		{0, -1},
		{-1, 0},
		{0, 1},
		{1, 0},
	}

	var dfs func(i, j int)
	dfs = func(i, j int) {
		was[i][j] = true
		for k := 0; k < 4; k++ {
			di, dj := steps[k][0], steps[k][1]
			ni, nj := i+di, j+dj
			if ni < 0 || ni == n || nj < 0 || nj == m {
				continue
			}
			if !was[ni][nj] && s[ni][nj] == '#' {
				dfs(ni, nj)
			}
		}
	}

	res := 0
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if !was[i][j] && s[i][j] == '#' {
				dfs(i, j)
				res++
			}
		}
	}
	fmt.Fprintln(out, res)

}

package main

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

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

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

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

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

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

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

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

	return i
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, m := readTwoNums(reader)

	grid := make([]string, n)
	for i := 0; i < n; i++ {
		grid[i], _ = reader.ReadString('\n')
		grid[i] = grid[i][:m]
	}

	fmt.Println(solve(n, m, grid))
}

var dd = []int{-1, 0, 1, 0, -1}

func solve(n, m int, grid []string) int {
	col := make([]bool, m)

	var black int

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' {
				black++
				// black
				if i > 0 && grid[i-1][j] == '.' {
					// white
					if col[j] {
						// has another black cell on top of white
						return -1
					}
				}
				col[j] = true
			}
		}
	}

	if black == 0 {
		return 0
	}

	for j := 0; j < m; j++ {
		if !col[j] {
			return -1
		}
	}

	row := make([]bool, n)

	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if grid[i][j] == '#' {
				if j > 0 && grid[i][j-1] == '.' {
					if row[i] {
						return -1
					}
				}
				row[i] = true
			}
		}
	}

	for i := 0; i < n; i++ {
		if !row[i] {
			return -1
		}
	}

	// safe

	vis := make([][]bool, n)
	for i := 0; i < n; i++ {
		vis[i] = make([]bool, m)
	}

	var dfs func(x, y int)

	dfs = func(x, y int) {
		for k := 0; k < 4; k++ {
			u, v := x+dd[k], y+dd[k+1]
			if u < 0 || u >= n || v < 0 || v >= m {
				continue
			}
			if grid[u][v] == '#' && !vis[u][v] {
				vis[u][v] = true
				dfs(u, v)
			}
		}
	}

	var cnt int

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' && !vis[i][j] {
				cnt++
				vis[i][j] = true
				dfs(i, j)
			}
		}
	}

	return cnt
}

package main

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

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

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

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

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

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

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

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

	return i
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, m := readTwoNums(reader)

	grid := make([]string, n)
	for i := 0; i < n; i++ {
		grid[i], _ = reader.ReadString('\n')
		grid[i] = grid[i][:m]
	}

	fmt.Println(solve(n, m, grid))
}

var dd = []int{-1, 0, 1, 0, -1}

func solve(n, m int, grid []string) int {
	col := make([]bool, m)

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' {
				// black
				if i > 0 && grid[i-1][j] == '.' {
					// white
					if col[j] {
						// has another black cell on top of white
						return -1
					}
				}
				col[j] = true
			}
		}
	}

	row := make([]bool, n)

	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if grid[i][j] == '#' {
				if j > 0 && grid[i][j-1] == '.' {
					if row[i] {
						return -1
					}
				}
				row[i] = true
			}
		}
	}

	var c1, c2 int

	for j := 0; j < m; j++ {
		if !col[j] {
			c1++
		}
	}

	for i := 0; i < n; i++ {
		if !row[i] {
			c2++
		}
	}

	if c1 > 0 && c1 < m && c2 == n {
		return -1
	}

	if c2 > 0 && c2 < n && c1 == m {
		return -1
	}

	// safe

	vis := make([][]bool, n)
	for i := 0; i < n; i++ {
		vis[i] = make([]bool, m)
	}

	var dfs func(x, y int)

	dfs = func(x, y int) {
		for k := 0; k < 4; k++ {
			u, v := x+dd[k], y+dd[k+1]
			if u < 0 || u >= n || v < 0 || v >= m {
				continue
			}
			if grid[u][v] == '#' && !vis[u][v] {
				vis[u][v] = true
				dfs(u, v)
			}
		}
	}

	var cnt int

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' && !vis[i][j] {
				cnt++
				vis[i][j] = true
				dfs(i, j)
			}
		}
	}

	return cnt
}

package main

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

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

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

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

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

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

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

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

	return i
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, m := readTwoNums(reader)

	grid := make([]string, n)
	for i := 0; i < n; i++ {
		grid[i], _ = reader.ReadString('\n')
		grid[i] = grid[i][:m]
	}

	fmt.Println(solve(n, m, grid))
}

var dd = []int{-1, 0, 1, 0, -1}

func solve(n, m int, grid []string) int {
	col := make([]bool, m)

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' {
				// black
				if i > 0 && grid[i-1][j] == '.' {
					// white
					if col[j] {
						// has another black cell on top of white
						return -1
					}
				}
				col[j] = true
			}
		}
	}

	row := make([]bool, n)

	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if grid[i][j] == '#' {
				if j > 0 && grid[i][j-1] == '.' {
					if row[i] {
						return -1
					}
				}
				row[i] = true
			}
		}
	}

	// safe

	vis := make([][]bool, n)
	for i := 0; i < n; i++ {
		vis[i] = make([]bool, m)
	}

	var dfs func(x, y int)

	dfs = func(x, y int) {
		for k := 0; k < 4; k++ {
			u, v := x+dd[k], y+dd[k+1]
			if u < 0 || u >= n || v < 0 || v >= m {
				continue
			}
			if grid[u][v] == '#' && !vis[u][v] {
				vis[u][v] = true
				dfs(u, v)
			}
		}
	}

	var cnt int

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' && !vis[i][j] {
				cnt++
				vis[i][j] = true
				dfs(i, j)
			}
		}
	}

	return cnt
}

package main

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

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

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

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

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

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

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

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

	return i
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, m := readTwoNums(reader)

	grid := make([]string, n)
	for i := 0; i < n; i++ {
		grid[i], _ = reader.ReadString('\n')
		grid[i] = grid[i][:m]
	}

	fmt.Println(solve(n, m, grid))
}

var dd = []int{-1, 0, 1, 0, -1}

func solve(n, m int, grid []string) int {
	col := make([]bool, m)

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' {
				// black
				if i > 0 && grid[i-1][j] == '.' {
					// white
					if col[j] {
						// has another black cell on top of white
						return -1
					}
				}
				col[j] = true
			}
		}
	}

	for j := 0; j < m; j++ {
		if !col[j] {
			return -1
		}
	}

	row := make([]bool, n)

	for j := 0; j < m; j++ {
		for i := 0; i < n; i++ {
			if grid[i][j] == '#' {
				if j > 0 && grid[i][j-1] == '.' {
					if row[i] {
						return -1
					}
				}
				row[i] = true
			}
		}
	}

	for i := 0; i < n; i++ {
		if !row[i] {
			return -1
		}
	}

	// safe

	vis := make([][]bool, n)
	for i := 0; i < n; i++ {
		vis[i] = make([]bool, m)
	}

	var dfs func(x, y int)

	dfs = func(x, y int) {
		for k := 0; k < 4; k++ {
			u, v := x+dd[k], y+dd[k+1]
			if u < 0 || u >= n || v < 0 || v >= m {
				continue
			}
			if grid[u][v] == '#' && !vis[u][v] {
				vis[u][v] = true
				dfs(u, v)
			}
		}
	}

	var cnt int

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if grid[i][j] == '#' && !vis[i][j] {
				cnt++
				vis[i][j] = true
				dfs(i, j)
			}
		}
	}

	return cnt
}


You play your favourite game yet another time. You chose the character you didn't play before. It has $$$str$$$ points of strength and $$$int$$$ points of intelligence. Also, at start, the character has $$$exp$$$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $$$1$$$ or raise intelligence by $$$1$$$).Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.

The first line contains the single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$T$$$ lines contain descriptions of queries — one per line. This line contains three integers $$$str$$$, $$$int$$$ and $$$exp$$$ ($$$1 \le str, int \le 10^8$$$, $$$0 \le exp \le 10^8$$$) — the initial strength and intelligence of the character and the number of free points, respectively.

Print $$$T$$$ integers — one per query. For each query print the number of different character builds you can create.

NoteIn the first query there are only three appropriate character builds: $$$(str = 7, int = 5)$$$, $$$(8, 4)$$$ and $$$(9, 3)$$$. All other builds are either too smart or don't use all free points.In the second query there is only one possible build: $$$(2, 1)$$$.In the third query there are two appropriate builds: $$$(7, 6)$$$, $$$(8, 5)$$$.In the fourth query all builds have too much brains.

package main

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

type point struct {
	x, y int
}

var ans int64

const (
	mod = int(1e9 + 7)
)

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	for i := 0; i < n; i++ {
		s, i, e := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &s, &i, &e)
		if i > s {
			e -= i - s
			s = i
			if e <= 0 {
				fmt.Fprintf(out, "%d\n", 0)
				continue
			}
		}
		x := 0
		x = (i + e - s) / 2
		for 2*x <= i+e-s {
			x++
		}
		if x < 0 {
			x = 0
		}
		//fmt.Println("debug", s, i, e, x)
		fmt.Fprintf(out, "%d\n", e-x+1)
	}
	return
}

func help(i int, n int, total int, t []int, g []int, cur int, c1, c2, c3 int, remaining int) {
	//fmt.Println(i, cur)
	if i == n {
		if cur == total {
			cal(c1, c2, c3)
		}
		return
	}
	if cur > total {
		return
	}
	if cur+remaining < total {
		return
	}
	switch g[i] {
	case 1:
		help(i+1, n, total, t, g, cur+t[i], c1+1, c2, c3, remaining-t[i])
	case 2:
		help(i+1, n, total, t, g, cur+t[i], c1, c2+1, c3, remaining-t[i])
	case 3:
		help(i+1, n, total, t, g, cur+t[i], c1, c2, c3+1, remaining-t[i])
	}
	help(i+1, n, total, t, g, cur, c1, c2, c3, remaining-t[i])
}

func cal(c1, c2, c3 int) {
	a := []int{c1, c2, c3}
	sort.Ints(a)
	tmp := int64(try(c1, c2, c3, 0))
	tmp %= int64(mod)
	for i := 1; i <= c1; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	for i := 1; i <= c2; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	for i := 1; i <= c3; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	ans += tmp
	ans %= int64(mod)
}

func try(a, b, c int, last int) int64 {
	if a == 0 && b == 0 && c == 0 {
		return 1
	}
	if a < 0 || b < 0 || c < 0 {
		return 0
	}
	if last == 0 {
		return try(a-1, b, c, 1) + try(a, b-1, c, 2) + try(a, b, c-1, 3)
	}
	if last == 1 {
		return try(a, b-1, c, 2) + try(a, b, c-1, 3)
	}
	if last == 2 {
		return try(a-1, b, c, 1) + try(a, b, c-1, 3)
	}
	return try(a-1, b, c, 1) + try(a, b-1, c, 2)
}

package main

import "fmt"

func main() {
	var T, str, intv, exp int
	fmt.Scanln(&T)
	for a:=0; a < T; a++ {
		fmt.Scanln(&str, &intv, &exp)
		if str+exp <= intv {
			fmt.Println(0)
		} else {
			ans := 0
			if str <= intv {
				exp = exp-(intv-str)
				str = intv
			}
			if str > intv {
				if str-intv > exp {
					ans = exp+1
					exp = 0
				} else {
					ans = str-intv
					exp -= str-intv
				}
			}
			if exp == 0 {
				fmt.Println(ans)
			} else {
				if exp%2 == 0 {
					fmt.Println(exp/2+ans)
				}else {
					fmt.Println(exp/2+ans+1)
				}
			}
		}
	}
}

package main

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

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	var n, a, b, c int
	fmt.Fscanf(reader, "%d\n", &n)
	for ; n != 0; n-- {
		fmt.Fscanf(reader, "%d%d%d\n", &a, &b, &c)

		a += c
		if a <= b {
			fmt.Println(0)
			continue
		}
		c++
		if c > (a-b+1)/2 {
			fmt.Println((a - b + 1) / 2)
		} else {
			fmt.Println(c)
		}
	}
}

/*
2
3 1 1
2 1 0
*/

package main

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

func main() {
	defer Flush()

	t := readi()
	for i := 0; i < t; i++ {
		st := readi()
		in := readi()
		ex := readi()
		println(solve(st, in, ex))
	}

}

func solve(st, in, ex int) int {

	k := sort.Search(ex+1, func(e int) bool {
		//eprintln(st, in, ex, e, in < st+e)
		return in+(ex-e) < st+e
	})
	return ex - k + 1
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// reverse slice in place
func reverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
}

// areverse: allocating reverse
func areverse(a []int) []int {
	b := make([]int, len(a))
	for i := range a {
		b[i] = a[len(a)-i-1]
	}
	return b
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

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

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

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

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

package main

import (
	"fmt"
)

func main() {
	var t, a, b, c int
	fmt.Scanf("%d\n", &t)
	for tc := 0; tc < t; tc++ {
		fmt.Scanf("%d %d %d\n", &a, &b, &c)

		lo := 0
		hi := c

		var lo2 int
		if b + c - a < 0 {
			lo2 = (b + c - a) / 2
		} else {
			lo2 = (b + c - a) / 2 + 1
		}
		if lo2 > lo {
			lo = lo2
		}

		answer := 0
		if lo <= hi {
			answer = hi - lo + 1
		}

		fmt.Println(answer)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	firstRow, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	var firstRowfields = strings.Fields(firstRow)
	n, err := strconv.Atoi(firstRowfields[0])
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < n; i++ {
			line, err := reader.ReadString('\n')
			ints, err := parseInts(line)
		if err != nil {
			log.Fatal(err)
		}

		result := solution(ints[0], ints[1], ints[2])

		fmt.Println(result)
	}
}

func parseInts(s string) ([]int, error) {
	var (
		fields = strings.Fields(s)
		ints   = make([]int, len(fields))
		err    error
	)
	for i, f := range fields {
		ints[i], err = strconv.Atoi(f)
		if err != nil {
			return nil, err
		}
	}
	return ints, nil
}

func solution(str int, int int, exp int) int {

	if exp <= 0 {
		if str > int {
			return 1
		} else {
			return 0
		}
	}

	if (str - int) > exp {
		return exp + 1
	}

	exp -= (int-str)

	if exp <= 0 {
		return 0
	}

	if exp%2 == 1 {
		return exp/2 + 1
	} else {
		return exp/2
	}

	return 0
}

package main

import (
  "fmt"
)

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

func main() {
  var t int

  fmt.Scan(&t)

  var a,b,c int64

  for t > 0 {
    //fmt.Println(t)
    var temp float64

    fmt.Scan(&a, &b, &c)

    //fmt.Println(a,b,c)

    temp = float64(c-a+b)/2
    //fmt.Println(temp,c)

    if temp < 0 {
      fmt.Println(c+1)
    } else {
      fmt.Println(max(0,c-int64(temp)))
    }

    t -= 1
  }

}

package main

import "fmt"

func main() {
	var T, s, i, e int
	fmt.Scan(&T)
	for t := 0; t < T; t++ {
		fmt.Scan(&s, &i, &e)
		least := (s+i+e)/2 + 1
		if s+e < least {
			fmt.Println(0)
			continue
		}
		ans := min(e+1, s+e-least+1)
		fmt.Println(ans)
	}
}

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

package main

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

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

func main() {

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

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

	for i := 0; i < t; i++ {
		var ss, ii, ee int64
		fmt.Fscanf(reader, "%d %d %d\n", &ss, &ii, &ee)
		if ss <= ii {
			ee -= (ii - ss)
			ss = ii
			if ee <= 0 {
				fmt.Println(0)
			} else {
				if ee % 2 == 0 {
					fmt.Println(ee / 2)
				} else {
					fmt.Println((ee + 1) / 2)
				}
			}
		} else {
			if ss - ii > ee {
				fmt.Println(ee + 1)
			} else {
				sss := ss - ii + (ee - (ss - ii) + 1)  / 2
				fmt.Println(sss)
			}

		}
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1217/problem/A

package main

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

func Solve(str int, intel int, exp int) int {
	if str+exp < intel {
		return 0
	}
	if intel+exp < str {
		return exp + 1
	}
	x := (intel + exp - str) / 2
	return exp - x
}

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

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0; i < T; i++ {
		var str, intel, exp int
		fmt.Fscanf(reader, "%d %d %d\n", &str, &intel, &exp)
		fmt.Fprintf(writer, "%d\n", Solve(str, intel, exp))
	}
}

package main

import (
    "fmt"
)

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

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

func main() {
    var t int
    fmt.Scan(&t)
    for ; t > 0; t-- {
        var s, i, e int
        fmt.Scan(&s, &i, &e)
        fmt.Println(Min(e + 1, Max(0, (s + e - i + 1) / 2)))
    }
}

package main

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

type point struct {
	x, y int
}

var ans int64

const (
	mod = int(1e9 + 7)
)

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	for i := 0; i < n; i++ {
		s, i, e := 0, 0, 0
		fmt.Fscanf(in, "%d %d %d\n", &s, &i, &e)
		if i > s {
			e -= i - s
			s = i
			if e <= 0 {
				fmt.Fprintf(out, "%d\n", 0)
				continue
			}
		}
		x := 0
		x = (i + e - s) / 2
		if 2*x <= i+e-s {
			x++
		}
		//fmt.Println("debug", s, i, e, x)
		fmt.Fprintf(out, "%d\n", e-x+1)
	}
	return
}

func help(i int, n int, total int, t []int, g []int, cur int, c1, c2, c3 int, remaining int) {
	//fmt.Println(i, cur)
	if i == n {
		if cur == total {
			cal(c1, c2, c3)
		}
		return
	}
	if cur > total {
		return
	}
	if cur+remaining < total {
		return
	}
	switch g[i] {
	case 1:
		help(i+1, n, total, t, g, cur+t[i], c1+1, c2, c3, remaining-t[i])
	case 2:
		help(i+1, n, total, t, g, cur+t[i], c1, c2+1, c3, remaining-t[i])
	case 3:
		help(i+1, n, total, t, g, cur+t[i], c1, c2, c3+1, remaining-t[i])
	}
	help(i+1, n, total, t, g, cur, c1, c2, c3, remaining-t[i])
}

func cal(c1, c2, c3 int) {
	a := []int{c1, c2, c3}
	sort.Ints(a)
	tmp := int64(try(c1, c2, c3, 0))
	tmp %= int64(mod)
	for i := 1; i <= c1; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	for i := 1; i <= c2; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	for i := 1; i <= c3; i++ {
		tmp *= int64(i)
		tmp %= int64(mod)
	}
	ans += tmp
	ans %= int64(mod)
}

func try(a, b, c int, last int) int64 {
	if a == 0 && b == 0 && c == 0 {
		return 1
	}
	if a < 0 || b < 0 || c < 0 {
		return 0
	}
	if last == 0 {
		return try(a-1, b, c, 1) + try(a, b-1, c, 2) + try(a, b, c-1, 3)
	}
	if last == 1 {
		return try(a, b-1, c, 2) + try(a, b, c-1, 3)
	}
	if last == 2 {
		return try(a-1, b, c, 1) + try(a, b, c-1, 3)
	}
	return try(a-1, b, c, 1) + try(a, b-1, c, 2)
}

package main

import "fmt"

func main() {
	var T, str, intv, exp int
	fmt.Scanln(&T)
	for a:=0; a < T; a++ {
		fmt.Scanln(&str, &intv, &exp)
		if str+exp <= intv {
			fmt.Println(0)
		} else {
			ans := 0
			if str <= intv {
				exp = exp-(intv-str)
				str = intv
			}
			if str > intv {
				if str-intv > exp {
					ans = exp+1
				} else {
					ans = str-intv
					exp -= str-intv
				}
			}
			if exp == 0 {
				fmt.Println(ans)
			} else {
				if exp%2 == 0 {
					fmt.Println(exp/2+ans)
				}else {
					fmt.Println(exp/2+ans+1)
				}
			}
		}
	}
}

package main

import "fmt"

func main() {
	var T, str, intv, exp int
	fmt.Scanln(&T)
	for a:=0; a < T; a++ {
		fmt.Scanln(&str, &intv, &exp)
		if str+exp <= intv {
			fmt.Println(0)
		} else {
			ans := 0
			if str <= intv {
				exp = exp-(intv-str)
				str = intv
				ans = 1
			}
			if str > intv {
				if str-intv > exp {
					ans = exp+1
				} else {
					ans = str-intv+1
					exp -= str-intv
				}
			}
			if exp == 0 {
				fmt.Println(ans)
			} else {
				if exp%2 == 0 {
					fmt.Println(exp/2-1+ans)
				}else {
					fmt.Println(exp/2+ans)
				}
			}
		}
	}
}

package main

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

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	var n, a, b, c int
	fmt.Fscanf(reader, "%d\n", &n)
	for ; n != 0; n-- {
		fmt.Fscanf(reader, "%d%d%d\n", &a, &b, &c)
		if c == 0 && a > b {
			fmt.Println(1)
			continue
		}

		a += c
		if c > (a-b+1)/2 {
			c = (a - b + 1) / 2
		}
		fmt.Println(c)
	}
}

package main

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

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	var n, a, b, c int
	fmt.Fscanf(reader, "%d\n", &n)
	for ; n != 0; n-- {
		fmt.Fscanf(reader, "%d%d%d\n", &a, &b, &c)
		if c == 0 {
			if a > b {
				fmt.Println(1)
			} else {
				fmt.Println(0)
			}
			continue
		}
		a += c
		if a <= b {
			fmt.Println(0)
			continue
		}
		if c > (a-b+1)/2 {
			fmt.Println((a - b + 1) / 2)
		} else {
			fmt.Println(c)
		}
	}
}

package main

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

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	var n, a, b, c int
	fmt.Fscanf(reader, "%d\n", &n)
	for ; n != 0; n-- {
		fmt.Fscanf(reader, "%d%d%d\n", &a, &b, &c)
		if c == 0 && a > b {
			fmt.Println(1)
			continue
		}

		a += c
		if (a-b+1)/2 < 0 {
			fmt.Println(0)
			continue
		}
		if c > (a-b+1)/2 {
			c = (a - b + 1) / 2
		}
		fmt.Println(c)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, a, b, c int
	fmt.Scanf("%d\n", &t)
	for tc := 0; tc < t; tc++ {
		fmt.Scanf("%d %d %d\n", &a, &b, &c)

		diff := b - a + 1;
		if diff < 0 {
			diff = 0
		}

		answer := 0
		if c >= diff {
			c -= diff
			a += diff
			answer = (a - b + c) / 2 + (a - b + c) % 2;
		}

		fmt.Println(answer)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, a, b, c int
	fmt.Scanf("%d\n", &t)
	for tc := 0; tc < t; tc++ {
		fmt.Scanf("%d %d %d\n", &a, &b, &c)

		diff := b - a + 1;
		if diff < 0 {
			diff = 0
		}

		answer := 0
		if c >= diff {
			c -= diff
			a += diff
			answer = c / 2 + 1;
		}

		fmt.Println(answer)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, a, b, c int
	fmt.Scanf("%d", &t)
	for tc := 0; tc < t; tc++ {
		fmt.Scanf("%d %d %d", &a, &b, &c)

		diff := b - a + 1;
		if diff < 0 {
			diff = 0
		}

		answer := 0
		if c >= diff {
			c -= diff
			a += diff
			answer = c / 2 + 1;
		}

		fmt.Println(answer)
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	firstRow, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	var firstRowfields = strings.Fields(firstRow)
	n, err := strconv.Atoi(firstRowfields[0])
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < n; i++ {
			line, err := reader.ReadString('\n')
			ints, err := parseInts(line)
		if err != nil {
			log.Fatal(err)
		}

		result := solution(ints[0], ints[1], ints[2])

		fmt.Println(result)
	}
}

func parseInts(s string) ([]int, error) {
	var (
		fields = strings.Fields(s)
		ints   = make([]int, len(fields))
		err    error
	)
	for i, f := range fields {
		ints[i], err = strconv.Atoi(f)
		if err != nil {
			return nil, err
		}
	}
	return ints, nil
}

func solution(str int, int int, exp int) int {

	if exp <= 0 {
		if str > int {
			return 1
		} else {
			return 0
		}
	}

	exp -= (int-str)

	if exp <= 0 {
		return 0
	}

	if exp%2 == 1 {
		return exp/2 + 1
	} else {
		return exp/2
	}

	return 0
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	firstRow, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	var firstRowfields = strings.Fields(firstRow)
	n, err := strconv.Atoi(firstRowfields[0])
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < n; i++ {
			line, err := reader.ReadString('\n')
			ints, err := parseInts(line)
		if err != nil {
			log.Fatal(err)
		}

		result := solution(ints[0], ints[1], ints[2])

		fmt.Println(result)
	}
}

func parseInts(s string) ([]int, error) {
	var (
		fields = strings.Fields(s)
		ints   = make([]int, len(fields))
		err    error
	)
	for i, f := range fields {
		ints[i], err = strconv.Atoi(f)
		if err != nil {
			return nil, err
		}
	}
	return ints, nil
}

func solution(str int, int int, exp int) int {

	exp -= (int-str)

	if exp <= 0 {
		return 0
	}

	if exp%2 == 1 {
		return exp/2 + 1
	} else {
		return exp/2
	}

	return 0
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	firstRow, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	var firstRowfields = strings.Fields(firstRow)
	n, err := strconv.Atoi(firstRowfields[0])
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < n; i++ {
			line, err := reader.ReadString('\n')
			ints, err := parseInts(line)
		if err != nil {
			log.Fatal(err)
		}

		result := solution(ints[0], ints[1], ints[2])

		fmt.Println(result)
	}
}

func parseInts(s string) ([]int, error) {
	var (
		fields = strings.Fields(s)
		ints   = make([]int, len(fields))
		err    error
	)
	for i, f := range fields {
		ints[i], err = strconv.Atoi(f)
		if err != nil {
			return nil, err
		}
	}
	return ints, nil
}

func solution(str int, int int, exp int) int {

	if exp <= 0 {
		return 0
	}

	exp -= (int-str)

	if exp <= 0 {
		return 0
	}

	if exp%2 == 1 {
		return exp/2 + 1
	} else {
		return exp/2
	}

	return 0
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	firstRow, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	var firstRowfields = strings.Fields(firstRow)
	n, err := strconv.Atoi(firstRowfields[0])
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < n; i++ {
			line, err := reader.ReadString('\n')
			ints, err := parseInts(line)
		if err != nil {
			log.Fatal(err)
		}

		result := solution(ints[0], ints[1], ints[2])

		fmt.Println(result)
	}
}

func parseInts(s string) ([]int, error) {
	var (
		fields = strings.Fields(s)
		ints   = make([]int, len(fields))
		err    error
	)
	for i, f := range fields {
		ints[i], err = strconv.Atoi(f)
		if err != nil {
			return nil, err
		}
	}
	return ints, nil
}

func solution(str int, int int, exp int) int {

	exp -= (int-str)

	if exp%2 == 1 {
		return exp/2 + 1
	} else {
		return exp/2
	}

	return 0
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	firstRow, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}

	var firstRowfields = strings.Fields(firstRow)
	n, err := strconv.Atoi(firstRowfields[0])
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < n; i++ {
			line, err := reader.ReadString('\n')
			ints, err := parseInts(line)
		if err != nil {
			log.Fatal(err)
		}

		result := solution(ints[0], ints[1], ints[2])

		fmt.Println(result)
	}
}

func parseInts(s string) ([]int, error) {
	var (
		fields = strings.Fields(s)
		ints   = make([]int, len(fields))
		err    error
	)
	for i, f := range fields {
		ints[i], err = strconv.Atoi(f)
		if err != nil {
			return nil, err
		}
	}
	return ints, nil
}

func solution(str int, int int, exp int) int {

    delta := str - int

	if exp <= 0 {
		if delta > 0 {
			return 1
		} else {
			return 0
		}
	}

	if delta > 0 {

		if delta > exp {
			return exp+1
		} else {
			delta = -delta
		}
	} else {
		exp -= delta

		if exp <= 0 {
			return 0
		}

		if exp%2 == 1 {
			return exp/2 + 1
		} else {
			return exp/2
		}
	}

	return 0
}

package main

import (
  "fmt"
)

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

func main() {
  var t int

  fmt.Scan(&t)

  var a,b,c int64

  for t > 0 {
    //fmt.Println(t)
    var temp float32

    fmt.Scan(&a, &b, &c)

    //fmt.Println(a,b,c)

    temp = float32(c-a+b)/2
    //fmt.Println(temp,c)

    if temp < 0 {
      fmt.Println(c+1)
    } else {
      fmt.Println(max(0,c-int64(temp)))
    }

    t -= 1
  }

}

// Author: sighduck
// URL: https://codeforces.com/contest/1217/problem/A

package main

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

func Solve(str int, intel int, exp int) int {
	if exp == 0 && str > intel {
		return 1
	} else if exp == 0 {
		return 0
	}
	x := (intel + exp - str) / 2
	if x > exp {
		return 0
	}
	if x < 0 {
		return exp
	}
	return exp - x
}

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

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0; i < T; i++ {
		var str, intel, exp int
		fmt.Fscanf(reader, "%d %d %d\n", &str, &intel, &exp)
		fmt.Fprintf(writer, "%d\n", Solve(str, intel, exp))
	}
}

// Author: sighduck
// URL: https://codeforces.com/contest/1217/problem/A

package main

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

func Solve(str int, intel int, exp int) int {
	if exp == 0 && str > intel {
		return 1
	} else if exp == 0 {
		return 0
	}
	x := (intel + exp - str) / 2
	if x < 0 {
		return exp
	}
	return exp - x
}

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

	var T int
	fmt.Fscanf(reader, "%d\n", &T)
	for i := 0; i < T; i++ {
		var str, intel, exp int
		fmt.Fscanf(reader, "%d %d %d\n", &str, &intel, &exp)
		fmt.Fprintf(writer, "%d\n", Solve(str, intel, exp))
	}
}

package main

import (
    "fmt"
)

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

func main() {
    var t int
    fmt.Scan(&t)
    for ; t > 0; t-- {
        var s, i, e int
        fmt.Scan(&s, &i, &e)
        fmt.Println(Max(0, (s + e - i + 1) / 2))
    }
}