You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.

The following definition of a regular bracket sequence is well-known, so you can be familiar with it.

Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s_1 and s_2 be a RBS then the strings <s_1>s_2, {s_1}s_2, [s_1]s_2, (s_1)s_2 are also RBS.

For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.

Determine the least number of replaces to make the string s RBS.


-----Input-----

The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 10^6.


-----Output-----

If it's impossible to get RBS from s print Impossible.

Otherwise print the least number of replaces needed to get RBS from s.


-----Examples-----
Input
[<}){}

Output
2
Input
{()}[]

Output
0
Input
]]

Output
Impossible

Difficulty: MEDIUM

Solutions:
from collections import *
from itertools import *
from random import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from re import *
from sys import *

def fast():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def output(answer):
	stdout.write(str(answer))
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
cnt = 0
arr = fast()
opn = {'(': ')', '<': '>', '[': ']', '{': '}'}
openBract = 0
que = deque()
for i in arr:
	if i in opn:
		openBract += 1
		que.append(opn[i])
	else:
		try:
			x = que.pop()
			if i != x:
				cnt += 1
				que.appendleft(x)
		except:
			print('Impossible')
			exit()
print(cnt if openBract == len(arr) - openBract else 'Impossible')


import sys
s = input()
OPENING = ('<', '{', '[', '(')
CLOSING = ('>', '}', ']', ')')
result = 0
stack = []
for c in s:
	if c in OPENING:
		stack.append(c)
	elif stack:
		last_br = stack.pop()
		if c != CLOSING[OPENING.index(last_br)]:
			result += 1
	else:
		print('Impossible')
		return
print('Impossible' if stack else result)


s = input()
otc = {'[': ']', '{': '}', '(': ')', '<': '>'}
cto = {'>': '<', '}': '{', ')': '(', ']': '['}
stack = []
res1 = 0
for c in s:
	if c in otc.keys():
		stack.append(c)
		continue
	if len(stack) == 0:
		res1 = -1
		break
	o = stack[len(stack) - 1]
	stack.pop()
	if c == otc[o]:
		continue
	else:
		res1 += 1
if res1 == -1 or len(stack) != 0:
	print('Impossible')
else:
	print(res1)


S = input()
N = len(S)
lt = {'[', '(', '{', '<'}
rt = {']', ')', '}', '>'}
stack = []
res = 0
for i in range(N):
	if S[i] in lt:
		stack.append(i)
	elif not stack:
		print('Impossible')
		break
	else:
		c = stack.pop()
		if S[c] + S[i] not in {'[]', '()', '{}', '<>'}:
			res += 1
else:
	if not stack:
		print(res)
	else:
		print('Impossible')


opening = set(['(', '<', '[', '{'])
closing = set([')', '>', ']', '}'])

def im_noob(s):
	ans = 0
	l = 0
	stack = []
	for i in s:
		if i in opening:
			if i == '[':
				stack.append(']')
			elif i == '<':
				stack.append('>')
			elif i == '{':
				stack.append('}')
			elif i == '(':
				stack.append(')')
			l += 1
		else:
			try:
				if i != stack[l - 1]:
					ans += 1
					stack.pop()
					l -= 1
				else:
					stack.pop()
					l -= 1
			except:
				return 'Impossible'
	if l > 0:
		return 'Impossible'
	return ans
s = input()
print(im_noob(s))


from collections import deque

def f(s1):
	return s1 in '<{[('
s = list(input())
d = deque([])
ans = 0
for i in s:
	if f(i):
		d.append(i)
	elif len(d) == 0:
		print('Impossible')
		exit()
	elif ord(d[-1]) + 2 == ord(i) or ord(d[-1]) + 1 == ord(i):
		d.pop()
	else:
		d.pop()
		ans += 1
if len(d) > 0:
	print('Impossible')
	exit()
print(ans)


import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
s = input()
cnt = 0
st = []
for elem in s:
	if elem in '([{<':
		st.append(elem)
	else:
		if len(st) == 0:
			print('Impossible')
			break
		elem2 = st.pop()
		if elem2 + elem not in '()[]{}<>':
			cnt += 1
else:
	if len(st) == 0:
		print(cnt)
	else:
		print('Impossible')


from sys import stdin
from collections import deque
lst = deque(stdin.readline().strip())
opened = '([<{'
closed = ')]>}'
stack = []
change = 0
for i in lst:
	stack.append(i)
	if len(stack) >= 2:
		if i in closed and stack[-2] in opened:
			if closed.index(i) != opened.index(stack[-2]):
				change += 1
			del stack[-2:]
		elif i in closed and stack[-2] in closed:
			change = 'Impossible'
			break
if len(stack) != 0:
	change = 'Impossible'
print(change)


s = input()
(left, ans) = (list(), 0)

def type(ch):
	if ch in '<>':
		return 0
	if ch in '{}':
		return 1
	if ch in '[]':
		return 2
	if ch in '()':
		return 3
for ch in s:
	if ch in '<{[(':
		left.append(ch)
	else:
		if len(left) == 0:
			print('Impossible')
			exit(0)
		ch_left = left.pop()
		if type(ch) != type(ch_left):
			ans += 1
print(ans if len(left) == 0 else 'Impossible')


s = input()
open = '({<['
close = ')}>]'
clopen = {']': '[', '}': '{', '>': '<', ')': '('}
ir2 = 0
ir = 0
if len(s) % 2 == 1:
	print('Impossible')
	return
else:
	l = []
	for i in range(len(s)):
		if s[i] in open:
			ir += 1
			l.append(s[i])
		else:
			ir -= 1
			if ir == -1:
				print('Impossible')
				return
			p = l.pop()
			if clopen[s[i]] != p:
				ir2 += 1
if ir != 0:
	print('Impossible')
	return
else:
	print(ir2)


k = input()
p = 0
q = []
for i in range(len(k)):
	if k[i] == '(' or k[i] == '{' or k[i] == '[' or (k[i] == '<'):
		q.append(k[i])
	else:
		if len(q) <= 0:
			print('Impossible')
			exit(0)
		m = q.pop()
		if k[i] == ')' and m == '(':
			continue
		elif k[i] == '}' and m == '{':
			continue
		elif k[i] == ']' and m == '[':
			continue
		elif k[i] == '>' and m == '<':
			continue
		else:
			p += 1
if len(q) != 0:
	print('Impossible')
	exit(0)
print(p)


(k, s) = (0, [])
for q in input():
	i = '[{<(]}>)'.find(q)
	if i > 3:
		if not s:
			s = 1
			break
		if s.pop() != i - 4:
			k += 1
	else:
		s += [i]
print('Impossible' if s else k)


def main(s: str):
	if len(s) == 0 or len(s) % 2 != 0:
		return 'Impossible'
	leftchr = ['(', '{', '<', '[']
	stack = list()
	opdict = {'(': ')', '{': '}', '<': '>', '[': ']'}
	mix = 0
	for oper in s:
		if oper in leftchr:
			stack.append(oper)
		else:
			try:
				top = stack.pop()
			except:
				return 'Impossible'
			if opdict.get(top) != oper:
				mix += 1
	if len(stack) != 0:
		return 'Impossible'
	return mix
s = str(input())
print(main(s))


s = list(input())
d = {'(': ')', '[': ']', '<': '>', '{': '}'}
stack = []
ans = 0
c = 0
for i in s:
	if i in d.keys():
		stack.append(i)
		c += 1
	else:
		c -= 1
		if c < 0:
			print('Impossible')
			exit()
		b = stack.pop()
		if i != d[b]:
			ans += 1
if c == 0:
	print(ans)
else:
	print('Impossible')


s = input()
d = []
ans = 0
for f in s:
	if f in '{[<(':
		d.append(f)
	elif f == ')' and d and (d[-1] == '('):
		d.pop()
	elif f == '}' and d and (d[-1] == '{'):
		d.pop()
	elif f == ']' and d and (d[-1] == '['):
		d.pop()
	elif f == '>' and d and (d[-1] == '<'):
		d.pop()
	elif not d:
		print('Impossible')
		break
	else:
		ans += 1
		d.pop()
else:
	if not d:
		print(ans)
	else:
		print('Impossible')


def solution(s):
	st = []
	ans = 0
	for (i, j) in enumerate(s):
		if j in open:
			st.append((i, j))
		if j in close:
			if not st:
				return 'Impossible'
			(q, w) = st.pop()
			if close.index(j) != open.index(w):
				ans += 1
	if st:
		return 'Impossible'
	return ans
s = str(input())
open = ['(', '{', '<', '[']
close = [')', '}', '>', ']']
print(solution(s))


from collections import deque
import sys
strv = list(input())
if len(strv) % 2 != 0:
	print('Impossible')
	return
openQ = deque([])
y = 0
for brk in strv:
	if brk in ['[', '{', '(', '<']:
		openQ.append(brk)
	elif len(openQ) >= 1:
		lastIn = openQ.pop()
		if brk == '}':
			if lastIn != '{':
				y = y + 1
		elif brk == ']':
			if lastIn != '[':
				y = y + 1
		elif brk == ')':
			if lastIn != '(':
				y = y + 1
		elif brk == '>':
			if lastIn != '<':
				y = y + 1
	else:
		y = 'Impossible'
		break
if len(openQ) == 0:
	print(y)
else:
	print('Impossible')


def main(s: str):
	if len(s) == 0:
		return 'Impossible'
	leftchr = ['(', '{', '<', '[']
	rightchr = [')', '}', '>', ']']
	stack = list()
	opdict = {'(': ')', '{': '}', '<': '>', '[': ']'}
	mix = 0
	for oper in s:
		if oper in leftchr:
			stack.append(oper)
		elif oper in rightchr:
			if len(stack) == 0:
				return 'Impossible'
			else:
				top = stack.pop()
			if opdict.get(top) != oper:
				mix += 1
	if len(stack) != 0:
		return 'Impossible'
	return mix
s = str(input())
print(main(s))


s = input()
stack = []
answer = 0
corrects = ('[]', '{}', '<>', '()')
for i in s:
	if i == '[' or i == '{' or i == '<' or (i == '('):
		stack.append(i)
	elif len(stack) == 0:
		print('Impossible')
		exit()
	else:
		top = stack.pop()
		if top + i not in corrects:
			answer += 1
print(answer if len(stack) == 0 else 'Impossible')


s = input()
s1 = []
for i in s:
	if i == '(':
		s1.append(1)
	elif i == '[':
		s1.append(2)
	elif i == '{':
		s1.append(3)
	elif i == '<':
		s1.append(4)
	elif i == ')':
		s1.append(-1)
	elif i == ']':
		s1.append(-2)
	elif i == '}':
		s1.append(-3)
	else:
		s1.append(-4)
m = []
c = 0
for char in s1:
	if len(m) > 0:
		if m[-1] > 0:
			if m[-1] == -char:
				m.pop()
			elif char < 0:
				m.pop()
				c += 1
			else:
				m.append(char)
		else:
			m.append(char)
	else:
		m.append(char)
if len(m) > 0:
	print('Impossible')
else:
	print(c)


def getresult(mystr):
	stack = []
	dictionary = {'<': '>', '{': '}', '[': ']', '(': ')'}
	result = 0
	for x in mystr:
		if x in dictionary:
			stack.append(x)
		elif len(stack) == 0:
			return 'Impossible'
		elif dictionary[stack.pop()] != x:
			result += 1
		else:
			pass
	if len(stack) == 0:
		return result
	else:
		return 'Impossible'
mystr = input()
print(getresult(mystr))


import sys
import math
import collections
import heapq
import decimal
s = input()
n = len(s)
c = 0
c1 = 1
for i in range(n):
	if s[i] in ['(', '<', '{', '[']:
		c += 1
	else:
		c -= 1
	if c < 0:
		c1 = 0
		break
if c1 == 0 or c != 0:
	print('Impossible')
else:
	l = []
	c1 = 1
	d = {'<': '>', '[': ']', '{': '}', '(': ')'}
	c = 0
	for i in range(n):
		if s[i] in ['(', '<', '{', '[']:
			l.append(s[i])
		elif l == []:
			c1 = 0
			break
		elif d[l[-1]] == s[i]:
			l.pop()
		else:
			l.pop()
			c += 1
	if c1 == 0:
		print('Impossible')
	else:
		print(c)


s = input()
brackets = {'<': '>', '{': '}', '[': ']', '(': ')'}
OPENING = '<{[('
CLOSING = '>}])'

def main(s):
	stack = []
	res = 0
	for c in s:
		if c in OPENING:
			stack.append(c)
		else:
			if stack:
				top = stack.pop()
				if top in OPENING:
					if brackets[top] != c:
						res += 1
					continue
			return 'Impossible'
	return res if not stack else 'Impossible'
print(main(s))


import sys
s = input()
stack = []
c = 0
brackets = {')': '(', ']': '[', '}': '{', '>': '<'}
for char in s:
	if char in brackets.values():
		stack.append(char)
	elif char in brackets.keys():
		if stack == []:
			print('Impossible')
			sys.exit()
		if brackets[char] != stack.pop():
			c = c + 1
if len(stack) != 0:
	print('Impossible')
	sys.exit(0)
print(c)


s = input()
cnt = 0
st = []
for elem in s:
	if elem in '([{<':
		st.append(elem)
	else:
		if len(st) == 0:
			print('Impossible')
			break
		elem2 = st.pop()
		if elem2 + elem not in '()[]{}<>':
			cnt += 1
else:
	if len(st) == 0:
		print(cnt)
	else:
		print('Impossible')


word = input()
BRACKETS = [('<', '>'), ('{', '}'), ('[', ']'), ('(', ')')]
OPENING = [b[0] for b in BRACKETS]
MATCH = dict(BRACKETS)
IMPOSSIBLE = 'Impossible'

def is_opening(w):
	return w in OPENING

def same_kind(wo, wc):
	return MATCH[wo] == wc

def solve(word):
	temp = []
	result = 0
	for w in word:
		if is_opening(w):
			temp.append(w)
		else:
			if len(temp) == 0:
				return IMPOSSIBLE
			wo = temp.pop()
			if not is_opening(wo):
				return IMPOSSIBLE
			if not same_kind(wo, w):
				result += 1
	if len(temp) != 0:
		return IMPOSSIBLE
	return result
print(solve(word))


try:

	def ad(expr):
		stack = []
		l = 0
		for char in expr:
			if char in ['(', '{', '[', '<']:
				stack.append(char)
			else:
				if not stack:
					return 'Impossible'
				current_char = stack.pop()
				if current_char == '(':
					if char != ')' and char == '}' or char == ']' or char == '>':
						l += 1
				if current_char == '{':
					if char != '}' and char == ')' or char == ']' or char == '>':
						l += 1
				if current_char == '[':
					if char != ']' and char == '}' or char == ')' or char == '>':
						l += 1
				if current_char == '<':
					if char != '>' and char == '}' or char == ')' or char == ']':
						l += 1
		if stack:
			return 'Impossible'
		return l
	s = input()
	print(ad(s))
except:
	pass


def p1(s):
	stack = []
	n = len(s)
	dic = {')': '(', '}': '{', ']': '[', '>': '<'}
	cnt = 0
	for x in s:
		if x not in dic:
			stack.append(x)
		elif not stack:
			return 'Impossible'
		elif dic[x] != stack.pop():
			cnt += 1
		else:
			pass
	if not stack:
		return cnt
	else:
		return 'Impossible'
s = list(input())
print(p1(s))


opening = set(['(', '<', '[', '{'])
closing = set([')', '>', ']', '}'])

def im_noob(s):
	o = c = boom = 0
	for i in s:
		if i in opening:
			o += 1
			boom += 1
		else:
			c += 1
			boom -= 1
		if boom < 0:
			return 'Impossible'
	if o != c:
		return 'Impossible'
	if boom != 0:
		return 'Impossible'
	ans = 0
	stack = []
	for i in s:
		if i in opening:
			if i == '[':
				stack.append(']')
			elif i == '<':
				stack.append('>')
			elif i == '{':
				stack.append('}')
			elif i == '(':
				stack.append(')')
		elif i != stack[len(stack) - 1]:
			ans += 1
			stack.pop()
		else:
			stack.pop()
	return ans
s = input()
print(im_noob(s))


s = input()
cnt = 0
closing = ['>', '}', ']', ')']
opening = ['<', '{', '[', '(']
oc = 0
cc = 0
for i in s:
	if i in opening:
		oc += 1
	else:
		cc += 1
if s[0] in closing or len(s) & 1 or oc != cc:
	print('Impossible')
else:
	stack = []
	for i in s:
		if i in closing:
			if len(stack) == 0:
				print('Impossible')
				exit(0)
			if opening[closing.index(i)] == stack[-1]:
				stack.pop()
			else:
				cnt += 1
				stack.pop()
		elif i in opening:
			stack.append(i)
	print(cnt)


bracket = input()
stack = []
bracketType = {'[': (0, ']'), '(': (0, ')'), '<': (0, '>'), '{': (0, '}'), ']': (1,), ')': (1,), '>': (1,), '}': (1,)}
res = 0
for b in bracket:
	if bracketType.get(b) == None:
		break
	elif bracketType.get(b)[0] == 0:
		stack.append(b)
	elif bracketType.get(b)[0] == 1:
		if len(stack) == 0:
			res = 'Impossible'
			break
		elif b != bracketType[stack.pop()][1]:
			res += 1
if len(stack):
	print('Impossible')
else:
	print(res)


stack = []
counter = 0
possible = True
for b in input():
	if b in '{<([':
		stack.append(b)
	else:
		if not stack:
			possible = False
			break
		popped = stack.pop()
		if b == '}' and popped != '{' or (b == '>' and popped != '<') or (b == ')' and popped != '(') or (b == ']' and popped != '['):
			counter += 1
if possible and (not stack):
	print(counter)
else:
	print('Impossible')


import sys
s = input()
stack = []
ans = 0
brackets = {'>': '<', '}': '{', ']': '[', ')': '('}
for c in s:
	if c in '>}])':
		if not stack:
			print('Impossible')
			exit()
		if stack[-1] != brackets[c]:
			ans += 1
		stack.pop()
	else:
		stack.append(c)
if len(stack):
	print('Impossible')
else:
	print(ans)


def opposto(s):
	if s == ')':
		return '('
	elif s == ']':
		return '['
	elif s == '}':
		return '{'
	else:
		return '<'

def f(s):
	n = [0] * 10 ** 6
	checker = 0
	cont = 0
	pos = 0
	for c in s:
		if c in ['(', '[', '{', '<']:
			n[checker] = c
			checker += 1
			pos += 1
		elif checker == 0:
			return 'Impossible'
		elif n[checker - 1] == opposto(c):
			checker -= 1
		else:
			checker -= 1
			cont += 1
	if pos == len(s) - pos:
		return cont
	else:
		return 'Impossible'
s = str(input())
print(f(s))


opening = {'[': ']', '<': '>', '{': '}', '(': ')'}
closing = {']': '[', '>': '<', '}': '{', ')': '('}
s = input()
stack = []
answ = 0
for c in s:
	if c in opening:
		stack.append(c)
	else:
		if len(stack) == 0:
			print('Impossible')
			return
		op = stack.pop()
		if c != opening[op]:
			answ += 1
if len(stack) != 0:
	print('Impossible')
	return
print(answ)


from sys import argv, exit

def rstr():
	return input()

def rint():
	return int(input())

def rints():
	return [int(i) for i in input().split(' ')]

def prnt(*args):
	if '-v' in argv:
		print(*args)
op = ['[', '{', '(', '<']
cl = [']', '}', ')', '>']
oppo = {']': '[', '}': '{', '>': '<', ')': '('}
oppo.update(zip(op, cl))
s = rstr()
opd = {c: 0 for c in op}
shtack = []
opi = 0
count = 0
for c in s:
	if c in cl:
		prnt(shtack)
		if not shtack:
			print('Impossible')
			exit(0)
		elif shtack[-1] == oppo[c]:
			shtack.pop()
		else:
			count += 1
			prnt(c, count)
			shtack.pop()
	else:
		shtack.append(c)
if shtack:
	print('Impossible')
	exit(0)
print(count)


S = input()
stack = []
count = 0
length = len(S)
flag = 0
if length % 2:
	flag = 1
else:
	for i in range(length):
		if S[i] == '<' or S[i] == '(' or S[i] == '{' or (S[i] == '['):
			stack.append(S[i])
		elif stack != []:
			if S[i] == '>' and stack.pop() != '<':
				count += 1
			elif S[i] == ')' and stack.pop() != '(':
				count += 1
			elif S[i] == '}' and stack.pop() != '{':
				count += 1
			elif S[i] == ']' and stack.pop() != '[':
				count += 1
			if flag:
				break
		else:
			flag = 1
			break
if flag != 0 or len(stack) != 0:
	print('Impossible')
else:
	print(count)


n = input()
res = []
cnt = 0
for i in n:
	if i in '(<[{':
		res.append(i)
	else:
		if not res:
			print('Impossible')
			break
		l = res.pop()
		if l + i not in ('[]', '()', '<>', '{}'):
			cnt += 1
else:
	if res:
		print('Impossible')
	else:
		print(cnt)


d = {'(': ')', '[': ']', '{': '}', '<': '>'}
(st, k) = ([], 0)
for i in input():
	if i in d:
		st.append(i)
	elif st:
		if d[st[-1]] != i:
			k += 1
		st.pop()
	else:
		print('Impossible')
		exit()
if st:
	print('Impossible')
	exit()
print(k)


import math
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
import bisect
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
d = defaultdict(int)
s = input().strip()
x = []
ans = 0
for i in range(len(s)):
	if s[i] == '[' or s[i] == '(' or s[i] == '{' or (s[i] == '<'):
		x.append(s[i])
	else:
		if len(x) == 0:
			print('Impossible')
			exit()
		if s[i] == ']':
			if x[-1] == '[':
				if len(x) == 0:
					print('Impossible')
					exit()
				x.pop()
			else:
				x.pop()
				ans += 1
		elif s[i] == '}':
			if x[-1] == '{':
				if len(x) == 0:
					print('Impossible')
					exit()
				x.pop()
			else:
				ans += 1
				x.pop()
		elif s[i] == '>':
			if x[-1] == '<':
				if len(x) == 0:
					print('Impossible')
					exit()
				x.pop()
			else:
				ans += 1
				x.pop()
		elif x[-1] == '(':
			if len(x) == 0:
				print('Impossible')
				exit()
			x.pop()
		else:
			x.pop()
			ans += 1
if len(x) != 0:
	print('Impossible')
else:
	print(ans)


seq = input()
a = []
fl = True
z = 0
for i in seq:
	if i == '<' or i == '[' or i == '{' or (i == '('):
		a.append(i)
	elif i == '>':
		if len(a) and a[-1] == '<':
			a.pop()
		elif len(a) and a[-1] != '<':
			a.pop()
			z += 1
		else:
			fl = False
			break
	elif i == ')':
		if len(a) and a[-1] == '(':
			a.pop()
		elif len(a) and a[-1] != '(':
			a.pop()
			z += 1
		else:
			fl = False
			break
	elif i == ']':
		if len(a) and a[-1] == '[':
			a.pop()
		elif len(a) and a[-1] != '[':
			a.pop()
			z += 1
		else:
			fl = False
			break
	elif i == '}':
		if len(a) and a[-1] == '{':
			a.pop()
		elif len(a) and a[-1] != '{':
			a.pop()
			z += 1
		else:
			fl = False
			break
if len(a):
	fl = False
if fl:
	print(z)
else:
	print('Impossible')


s = input()

def fun():
	t = ['[', '<', '{', '(']
	t1 = [']', '>', '}', ')']
	res = []
	summ = 0
	for i in range(len(s)):
		try:
			if s[i] in t:
				res.append(s[i])
			elif s[i] in t1:
				d = len(res) - 1
				if t.index(res[d]) != t1.index(s[i]):
					summ += 1
				res.pop(d)
		except:
			print('Impossible')
			break
	else:
		if len(res) == 0:
			print(summ)
		else:
			print('Impossible')
fun()


SYMBOLS = {'}': '{', ']': '[', ')': '(', '>': '<'}
(SYMBOLS_L, SYMBOLS_R) = (SYMBOLS.values(), SYMBOLS.keys())
arr = []
num = 0
left = 0
right = 0
flag = True
s = input()
for c in s:
	if c in SYMBOLS_L:
		arr.append(c)
		left += 1
	elif c in SYMBOLS_R:
		right += 1
		if len(arr) == 0:
			flag = False
			break
		elif arr[-1] == SYMBOLS[c]:
			arr.pop()
		else:
			num += 1
			arr.pop()
if right != left:
	flag = False
if flag:
	print(num)
else:
	print('Impossible')


s = list(input())

def isopen(c):
	return c == '<' or c == '{' or c == '[' or (c == '(')

def match(a, b):
	return a == '<' and b == '>' or (a == '{' and b == '}') or (a == '[' and b == ']') or (a == '(' and b == ')')

def isRbs():
	br = []
	if isopen(s[0]) == False:
		print('Impossible')
		return False
	count = 0
	for c in s:
		open = isopen(c)
		if open == True:
			br.append(c)
		elif len(br) > 0 and open == False:
			b = br.pop()
			if match(b, c) == False:
				count += 1
	if len(br) == 0:
		print(count)
	else:
		print('Impossible')
if len(s) % 2 != 0:
	print('Impossible')
else:
	isRbs()


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
l = []
a = '>}])'
b = '<{[('
r = 0
for i in minp():
	if i in a:
		if len(l) == 0:
			print('Impossible')
			exit(0)
		elif b[a.find(i)] != l.pop():
			r += 1
	else:
		l.append(i)
if len(l) == 0:
	print(r)
else:
	print('Impossible')


import sys
stack = []
count = 0
lr = {')': '(', '>': '<', ']': '[', '}': '{'}
for char in input():
	if char in lr.values():
		stack.append(char)
	elif char in lr.keys():
		if stack == []:
			print('Impossible')
			sys.exit()
		if lr[char] != stack.pop():
			count += 1
if len(stack) != 0:
	print('Impossible')
	sys.exit()
print(count)


def solve(s: str):
	stack = []
	answer = 0
	for c in s:
		if c in '([{<':
			stack.append(c)
		else:
			if not stack:
				print('Impossible')
				return
			b = stack[-1]
			if b == '(' and c != ')' or (b == '[' and c != ']') or (b == '{' and c != '}') or (b == '<' and c != '>'):
				answer += 1
			stack.pop()
	print('Impossible' if stack else answer)
s = input()
solve(s)


st = input()
c = 0
b = r = s = l = 0
for i in st:
	if i in ['[', '<', '{', '(']:
		c += 1
	else:
		c -= 1
		if c < 0:
			break
ans = 0
if c != 0:
	print('Impossible')
else:
	stack = []
	for i in st:
		if i in ['[', '<', '{', '(']:
			stack.append(i)
		elif stack[-1] == '(' and i == ')':
			stack.pop()
			continue
		elif stack[-1] == '<' and i == '>':
			stack.pop()
			continue
		elif stack[-1] == '{' and i == '}':
			stack.pop()
			continue
		elif stack[-1] == '[' and i == ']':
			stack.pop()
			continue
		else:
			stack.pop()
			ans += 1
	print(ans)


from functools import cmp_to_key
from collections import deque, Counter
from heapq import heappush, heappop
from math import log, ceil
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def ii():
	return int(inp())

def si():
	return str(inp())

def li(lag=0):
	l = list(map(int, inp().split()))
	if lag != 0:
		for i in range(len(l)):
			l[i] += lag
	return l

def mi(lag=0):
	matrix = list()
	for i in range(n):
		matrix.append(li(lag))
	return matrix

def lsi():
	return list(map(str, inp().split()))

def print_list(lista, space=' '):
	print(space.join(map(str, lista)))

def bisect_left(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] < x:
			left = mid + 1
		else:
			right = mid
	return left

def bisect_right(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] > x:
			right = mid
		else:
			left = mid + 1
	return left

def bisect_elements(a, x):
	return bisect_right(a, x) - bisect_left(a, x)
MOD = 10 ** 9 + 7
maxN = 5
FACT = [0] * maxN
INV_FACT = [0] * maxN

def add(x, y):
	return (x + y) % MOD

def multiply(x, y):
	return x * y % MOD

def power(x, y):
	if y == 0:
		return 1
	elif y % 2:
		return multiply(x, power(x, y - 1))
	else:
		a = power(x, y // 2)
		return multiply(a, a)

def inverse(x):
	return power(x, MOD - 2)

def divide(x, y):
	return multiply(x, inverse(y))

def allFactorials():
	FACT[0] = 1
	for i in range(1, maxN):
		FACT[i] = multiply(i, FACT[i - 1])

def inverseFactorials():
	n = len(INV_FACT)
	INV_FACT[n - 1] = inverse(FACT[n - 1])
	for i in range(n - 2, -1, -1):
		INV_FACT[i] = multiply(INV_FACT[i + 1], i + 1)

def coeffBinom(n, k):
	if n < k:
		return 0
	return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n - k]))

def create_graph(n, m, undirected=1, unweighted=1):
	graph = [[] for i in range(n)]
	if unweighted:
		for i in range(m):
			[x, y] = li(lag=-1)
			graph[x].append(y)
			if undirected:
				graph[y].append(x)
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			graph[x].append([y, w])
			if undirected:
				graph[y].append([x, w])
	return graph

def create_tree(n, unweighted=1):
	children = [[] for i in range(n)]
	if unweighted:
		for i in range(n - 1):
			[x, y] = li(lag=-1)
			children[x].append(y)
			children[y].append(x)
	else:
		for i in range(n - 1):
			[x, y, w] = li(lag=-1)
			w += 1
			children[x].append([y, w])
			children[y].append([x, w])
	return children

def dist(tree, n, A, B=-1):
	s = [[A, 0]]
	(massimo, massimo_nodo) = (0, 0)
	distanza = -1
	v = [-1] * n
	while s:
		(el, dis) = s.pop()
		if dis > massimo:
			massimo = dis
			massimo_nodo = el
		if el == B:
			distanza = dis
		for child in tree[el]:
			if v[child] == -1:
				v[child] = 1
				s.append([child, dis + 1])
	return (massimo, massimo_nodo, distanza)

def diameter(tree):
	(_, foglia, _) = dist(tree, n, 0)
	(diam, _, _) = dist(tree, n, foglia)
	return diam

def dfs(graph, n, A):
	v = [-1] * n
	s = [[A, 0]]
	v[A] = 0
	while s:
		(el, dis) = s.pop()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def bfs(graph, n, A):
	v = [-1] * n
	s = deque()
	s.append([A, 0])
	v[A] = 0
	while s:
		(el, dis) = s.popleft()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def parents_children_root_unrooted_tree(tree, n, root=0):
	q = deque()
	visited = [0] * n
	parent = [-1] * n
	children = [[] for i in range(n)]
	q.append(root)
	while q:
		all_done = 1
		visited[q[0]] = 1
		for child in tree[q[0]]:
			if not visited[child]:
				all_done = 0
				q.appendleft(child)
		if all_done:
			for child in tree[q[0]]:
				if parent[child] == -1:
					parent[q[0]] = child
					children[child].append(q[0])
			q.popleft()
	return (parent, children)

def all_longest_path_passing_from_node(parent, children, n):
	q = deque()
	visited = [len(children[i]) for i in range(n)]
	downwards = [[0, 0] for i in range(n)]
	upward = [1] * n
	longest_path = [1] * n
	for i in range(n):
		if not visited[i]:
			q.append(i)
			downwards[i] = [1, 0]
	while q:
		node = q.popleft()
		if parent[node] != -1:
			visited[parent[node]] -= 1
			if not visited[parent[node]]:
				q.append(parent[node])
		else:
			root = node
		for child in children[node]:
			downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse=True)[0:2]
	s = [node]
	while s:
		node = s.pop()
		if parent[node] != -1:
			if downwards[parent[node]][0] == downwards[node][0] + 1:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1])
			else:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0])
		longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1
		for child in children[node]:
			s.append(child)
	return longest_path
s = si()
n = len(s)
stack = list()
op = ['<', '(', '[', '{']
cl = ['>', ')', ']', '}']
match = {'<': '>', '(': ')', '[': ']', '{': '}'}
flag = True
count = 0
for i in range(n):
	if s[i] in op:
		stack.append(s[i])
	elif len(stack) == 0:
		flag = False
	else:
		last = stack.pop()
		if s[i] != match[last]:
			count += 1
if stack:
	flag = False
if flag == False:
	print('Impossible')
else:
	print(count)


from collections import deque
s = list(input())
d = deque()
r = 0
for i in range(len(s)):
	if s[i] in {'<', '{', '[', '('}:
		d.appendleft(s[i])
	else:
		if len(d) == 0:
			print('Impossible')
			exit()
		v = d.popleft()
		if v == '[' and s[i] != ']' or (v == '{' and s[i] != '}') or (v == '<' and s[i] != '>') or (v == '(' and s[i] != ')'):
			r += 1
if len(d) == 0:
	print(r)
else:
	print('Impossible')


def getResult(s):
	if not s:
		return 'Impossible'
	dic = {'(': ')', '[': ']', '{': '}', '<': '>'}
	res = 0
	stack = []
	for x in list(s):
		if x in dic:
			stack.append(x)
		elif len(stack) == 0:
			return 'Impossible'
		else:
			cur = stack.pop()
			if dic[cur] != x:
				res += 1
	return res if len(stack) == 0 else 'Impossible'
s = input()
print(str(getResult(s)))


import sys
s = input()
q = []
c = 0
for x in s:
	if x == '[' or x == '{' or x == '(' or (x == '<'):
		q.append(x)
	elif len(q) > 0 and (ord(x) == ord(q[-1]) + 2 or (x == ')' and q[-1] == '(')):
		q.pop(-1)
	elif len(q) > 0:
		q.pop(-1)
		c += 1
	else:
		print('Impossible')
		sys.exit(0)
print(c if len(q) == 0 else 'Impossible')


import sys
INF = 10 ** 18 + 3

def main():
	opening = '<{[('
	closing = '>}])'
	s = input()
	stack = []
	res = 0
	for (i, sym) in enumerate(s):
		if sym in opening:
			kind = opening.find(sym)
			stack.append(kind)
		else:
			kind = closing.find(sym)
			if not stack:
				res = -1
				break
			if stack[-1] != kind:
				res += 1
			del stack[-1]
	print('Impossible' if res == -1 or stack else res)

def set_input(file):
	global input
	input = lambda : file.readline().rstrip()

def set_output(file):
	global print
	lprint = print

	def print(*args, **kwargs):
		kwargs['file'] = kwargs.get('file', file)
		return lprint(*args, **kwargs)
is_mine = 'MINE' in sys.argv
set_input(open('input.txt', 'r') if is_mine else sys.stdin)
set_output(sys.stdout if is_mine else sys.stdout)
main()


s = input()
open = {'(', '{', '[', '<'}
res = 0
our = []
for elem in s:
	if elem in open:
		our.append(elem)
	elif our:
		res += abs(ord(elem) - ord(our[-1])) > 2
		our.pop()
	else:
		print('Impossible')
		exit()
if our:
	print('Impossible')
else:
	print(res)


import sys
s = input()
stack = []
piar = {'{': '}', '(': ')', '<': '>', '[': ']'}
ans = 0
for ch in s:
	if ch in piar.keys():
		stack.append(ch)
	else:
		if len(stack) == 0:
			print('Impossible')
			sys.exit()
		if piar[stack.pop()] != ch:
			ans += 1
if len(stack) != 0:
	print('Impossible')
	sys.exit()
print(ans)


IMPOSSIBLE = 'Impossible'
scopes = {'{': '}', '(': ')', '[': ']', '<': '>'}
stack = []
swap = 0
for i in input():
	if i in scopes:
		stack.append(i)
	elif stack:
		if scopes[stack[-1]] != i:
			swap += 1
		stack.pop()
	else:
		print(IMPOSSIBLE)
		break
else:
	print(swap if not stack else IMPOSSIBLE)


s = input()
sc = s
sc = sc.replace('<', '(').replace('[', '(').replace('{', '(')
sc = sc.replace('>', ')').replace(']', ')').replace('}', ')')

def chec(ss):
	c = 0
	for i in ss:
		if i == '(':
			c += 1
		if i == ')':
			c -= 1
		if c < 0:
			return False
	return c == 0
if chec(sc):
	l = []
	ans = 0
	for i in s:
		if i in '({[<':
			l.append(i)
		else:
			if i == ')':
				if l[-1] in '{[<':
					ans += 1
			if i == '}':
				if l[-1] in '([<':
					ans += 1
			if i == ']':
				if l[-1] in '{(<':
					ans += 1
			if i == '>':
				if l[-1] in '{[(':
					ans += 1
			l.pop()
	print(ans)
else:
	print('Impossible')


a = input()
s = []
replacements = 0
flag = 0
for i in range(len(a)):
	if a[i] in '({[<':
		s.append(a[i])
	else:
		if len(s) == 0:
			flag = 1
			break
		top = s.pop()
		if a[i] == '>' and top != '<':
			replacements += 1
		if a[i] == ')' and top != '(':
			replacements += 1
		if a[i] == ']' and top != '[':
			replacements += 1
		if a[i] == '}' and top != '{':
			replacements += 1
if len(s) > 0 or flag == 1:
	print('Impossible')
else:
	print(replacements)


s = input()
arr = []
l = len(s)
flag = False
cnt = 0
open = ['{', '[', '<', '(']
close = ['}', ']', '>', ')']
dic = {'{': '}', '[': ']', '<': '>', '(': ')'}
for i in range(l):
	if s[i] in open:
		arr.append(s[i])
	else:
		if arr == []:
			flag = True
			break
		if dic[arr[-1]] != s[i]:
			cnt += 1
		del arr[-1]
if flag or arr != []:
	print('Impossible')
else:
	print(cnt)


import re
import sys
exit = sys.exit
from bisect import bisect_left as bsl, bisect_right as bsr
from collections import Counter, defaultdict as ddict, deque
from functools import lru_cache
cache = lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum = enumerate
ri = lambda : int(rln())
ris = lambda : list(map(int, rfs()))
rln = sys.stdin.readline
rl = lambda : rln().rstrip('\n')
rfs = lambda : rln().split()
cat = ''.join
catn = '\n'.join
mod = 1000000007
d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]
d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]

def solve(s):
	mp = dict('<> {} [] ()'.split())
	ans = 0
	stk = []
	for c in s:
		if c in mp:
			stk.append(mp[c])
		else:
			if not stk:
				return -1
			if stk[-1] != c:
				ans += 1
			stk.pop()
	if stk:
		return -1
	return ans
s = rl()
ans = solve(s)
if ans == -1:
	print('Impossible')
else:
	print(ans)


def check_open(char):
	if char in '<{[(':
		return 1
	return 0

def find_type(char):
	if char in '()':
		return 0
	elif char in '[]':
		return 1
	elif char in '{}':
		return 2
	else:
		return 3
s = input()
stack = []
impossible = 0
count = 0
for char in s:
	if check_open(char):
		stack.append(char)
	else:
		if len(stack) == 0:
			impossible = True
			break
		if check_open(stack[-1]):
			if find_type(char) != find_type(stack[-1]):
				count += 1
			stack.pop()
		else:
			impossible = True
			break
if len(stack) != 0:
	impossible = True
if impossible:
	print('Impossible')
else:
	print(count)


s = input()
stack = []
res = 0
for c in s:
	if c in '(<[{':
		stack.append(c)
	else:
		if not stack:
			print('Impossible')
			break
		last = stack.pop()
		if last + c not in ('[]', '()', '<>', '{}'):
			res += 1
else:
	if stack:
		print('Impossible')
	else:
		print(res)


s = list(input())
d = {'(': ')', '[': ']', '<': '>', '{': '}'}
stack = []
cnt = 0
for elem in s:
	if elem in d.keys():
		stack.append(elem)
	else:
		if not stack:
			print('Impossible')
			exit()
		lb = stack.pop()
		if elem != d[lb]:
			cnt += 1
if not stack:
	print(cnt)
else:
	print('Impossible')


def stacks(brackets):
	opening = ['[', '{', '(', '<']
	closing = ['}', '{', '(', '<']
	stack = []
	changes = 0
	for b in brackets:
		if b in opening:
			stack.append(b)
		else:
			if len(stack) == 0:
				return -1
			last_brackets = stack.pop()
			if b == ']':
				if last_brackets != '[':
					changes += 1
			elif b == '}':
				if last_brackets != '{':
					changes += 1
			elif b == ')':
				if last_brackets != '(':
					changes += 1
			elif b == '>':
				if last_brackets != '<':
					changes += 1
			else:
				return -1
	if stack:
		return -1
	else:
		return changes
_in = input()
brackets = []
for b in _in:
	brackets.append(str(b))
v = stacks(brackets)
if v == -1:
	print('Impossible')
else:
	print(v)


opens = ['(', '{', '<', '[']
closes = [')', '}', '>', ']']

def solve(inp):
	stack = []
	res = 0
	for (i, x) in enumerate(inp):
		if x in opens:
			stack.append((i, x))
		if x in closes:
			if not stack:
				return 'Impossible'
			(ii, xx) = stack.pop()
			if closes.index(x) != opens.index(xx):
				res += 1
	if stack:
		return 'Impossible'
	return res
print(solve(input()))


from sys import stdin, stdout

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		s = R()[:-1]
		(brackets, cnt, check) = ([], 0, 0)
		for i in range(len(s)):
			if s[i] in ['(', '<', '{', '[']:
				brackets.append(s[i])
				check += 1
			else:
				if len(brackets) == 0:
					W('Impossible\n')
					return 0
				if s[i] == ')':
					if brackets[-1] != '(':
						cnt += 1
				elif s[i] == '>':
					if brackets[-1] != '<':
						cnt += 1
				elif s[i] == '}':
					if brackets[-1] != '{':
						cnt += 1
				elif s[i] == ']':
					if brackets[-1] != '[':
						cnt += 1
				check -= 1
				brackets.pop()
			if check < 0:
				W('Impossible\n')
				return 0
		if check:
			W('Impossible\n')
			return 0
		W('%d\n' % cnt)
		return 0

def main():
	s = SOLVE()
	s.solve()
main()


import sys

def rbs(mystr):
	stack = []
	kuohao = {'<': '>', '{': '}', '[': ']', '(': ')'}
	count = 0
	for x in mystr:
		if x in kuohao:
			stack.append(x)
		elif len(stack) == 0:
			return 'Impossible'
		elif kuohao[stack.pop()] != x:
			count += 1
		else:
			pass
	if len(stack) == 0:
		return count
	else:
		return 'Impossible'
mystr = input()
print(rbs(mystr))


s = input()
open_b = ['<', '{', '[', '(']
close_b = ['>', '}', ']', ')']
stack = []
d = {open_b[i]: close_b[i] for i in range(4)}
stack = []
ans = 0
for i in range(len(s)):
	stack.append(s[i])
	if len(stack) >= 2 and stack[-1] in close_b and (stack[-2] in open_b):
		if d[stack[-2]] != stack[-1]:
			ans = ans + 1
		stack.pop()
		stack.pop()
if len(stack):
	print('Impossible')
	exit()
print(ans)


s = input()

def check_str(s):
	brackets = {'<': '>', '{': '}', '[': ']', '(': ')'}
	res = 0
	stack = []
	for c in s:
		if c in brackets:
			stack.append(c)
		else:
			if stack:
				top = stack.pop()
				if top in brackets:
					if brackets[top] != c:
						res += 1
					continue
			return 'Impossible'
	if stack:
		return 'Impossible'
	return res
print(check_str(s))


s = input().strip()
braces = {'<': '>', '>': '<', '{': '}', '}': '{', '[': ']', ']': '[', '(': ')', ')': '('}
stack = []
answer = 0
for char in s:
	if char in '(<{[':
		stack.append(char)
	elif char in ')>}]':
		if not stack:
			print('Impossible')
			exit()
		lastBrace = stack.pop()
		if char != braces[lastBrace]:
			answer += 1
if not stack:
	print(answer)
else:
	print('Impossible')


import sys
from math import *

def input():
	return sys.stdin.readline().strip()
q = []
s = '>}])'
a = '<{[('
w = 0
for i in input():
	if i in s:
		if len(q) == 0:
			print('Impossible')
			exit(0)
		elif a[s.find(i)] != q.pop():
			w += 1
	else:
		q.append(i)
if len(q) == 0:
	print(w)
else:
	print('Impossible')


stk = []
ops = 0
f = 1
l = list(input())
for i in l:
	if i in '{<([':
		stk.append(i)
	else:
		if not stk:
			f = 0
			break
		c = stk[-1]
		if c + i in ['[]', '{}', '<>', '()']:
			stk.pop()
		else:
			ops += 1
			stk.pop()
if f == 1 and (not stk):
	print(ops)
else:
	print('Impossible')


s = input()
n = len(s)
br = {'<': '>', '{': '}', '[': ']', '(': ')'}
stack = []
flag = True
swaps = 0
for i in range(n):
	if s[i] in br:
		stack.append(s[i])
	elif stack:
		if br[stack.pop()] != s[i]:
			swaps += 1
	else:
		flag = False
		break
if len(stack) != 0:
	flag = False
print(swaps) if flag else print('Impossible')


s = input()
flag = 0
stack = []
op = ['<', '{', '[', '(']
ans = 0
for i in range(len(s)):
	if s[i] in op:
		flag += 1
	else:
		flag -= 1
	if flag < 0:
		print('Impossible')
		break
	if s[i] in op:
		stack.append(s[i])
	elif s[i] == '>' and stack.pop() != '<':
		ans += 1
	elif s[i] == '}' and stack.pop() != '{':
		ans += 1
	elif s[i] == ']' and stack.pop() != '[':
		ans += 1
	elif s[i] == ')' and stack.pop() != '(':
		ans += 1
else:
	if flag != 0:
		print('Impossible')
	else:
		print(ans)