A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.

The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.

Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.

If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

Input

The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

Output

Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

Examples

Input

1033


Output

33


Input

10


Output

0


Input

11


Output

-1

Note

In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.

Difficulty: HARD

Solutions:
import os
import sys
from io import BytesIO, IOBase

def main():
	a = list(map(int, list(input())))
	if sum(a) % 3 == 0:
		print(*a, sep='')
	else:
		n = len(a)
		temp = [[], [], []]
		realans = []
		for x in range(n):
			temp[a[x] % 3].append(x)
		left = sum(a) % 3
		temp[1].sort()
		temp[2].sort()
		realans = []
		if left == 1:
			boss = list(a)
			if len(temp[1]) > 0:
				boss.pop(temp[1][len(temp[1]) - 1])
				x = 0
				if len(set(boss)) == 1 and boss[0] == 0:
					realans.append([0])
				else:
					while x < len(boss) and boss[x] == 0:
						x += 1
					boss = boss[x:]
					realans.append(boss)
			if len(temp[2]) >= 2:
				a.pop(temp[2][len(temp[2]) - 1])
				a.pop(temp[2][len(temp[2]) - 2])
				x = 0
				if len(set(a)) == 1 and a[0] == 0:
					realans.append([0])
				else:
					while x < len(a) and a[x] == 0:
						x += 1
					a = a[x:]
					realans.append(a)
			realans.sort(key=lambda x: len(x), reverse=True)
			if len(realans) == 0:
				a = []
			else:
				a = realans[0]
			if len(a) == 0:
				print(-1)
			else:
				if len(set(a)) == 1 and a[0] == 0:
					print(0)
					return
				x = 0
				while a[x] == 0:
					x += 1
				print(*a[x:], sep='')
		elif left == 2:
			boss = list(a)
			if len(temp[2]) > 0:
				boss.pop(temp[2][len(temp[2]) - 1])
				x = 0
				if len(set(boss)) == 1 and boss[0] == 0:
					realans.append([0])
				else:
					while x < len(boss) and boss[x] == 0:
						x += 1
					boss = boss[x:]
					realans.append(boss)
			if len(temp[1]) > 1:
				a.pop(temp[1][len(temp[1]) - 1])
				a.pop(temp[1][len(temp[1]) - 2])
				x = 0
				if len(set(a)) == 1 and a[0] == 0:
					realans.append([0])
				else:
					while x < len(a) and a[x] == 0:
						x += 1
					a = a[x:]
					realans.append(a)
			realans.sort(key=lambda x: len(x), reverse=True)
			if len(realans) == 0:
				a = []
			else:
				a = realans[0]
			if len(a) == 0:
				print(-1)
			else:
				if len(set(a)) == 1 and a[0] == 0:
					print(0)
					return
				x = 0
				while a[x] == 0:
					x += 1
				print(*a[x:], sep='')
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = list(input())
leng = 0
mp = [0, 0, 0]
for x in n:
	leng += 1
	mp[int(x) % 3] += 1
tot = (mp[1] + 2 * mp[2]) % 3
if tot == 0:
	print(''.join(n))
	exit()
if mp[tot] == 0:
	do = tot ^ 3
	cnt = 2
elif mp[tot] == 1 and int(n[0]) % 3 == tot and (n[1:3] == ['0', '0']):
	do = tot ^ 3
	cnt = 2
	if mp[do] == 0:
		do = tot
		cnt = 1
else:
	do = tot
	cnt = 1
index = leng - 1
if cnt >= leng:
	print(-1)
	exit()
for x in range(cnt):
	while int(n[index]) % 3 != do:
		index -= 1
	n[index] = ''
	index -= 1
index = 0
ans = ''.join(n)
while ans[index] == '0' and index < leng - cnt - 1:
	index += 1
print(ans[index:])


def f(x, y):
	global n
	if d[x] and (d[x][-1] != 0 or n[1] != '0'):
		n = n[:d[x][-1]] + n[d[x][-1] + 1:]
	else:
		i = 0
		if d[x]:
			for i in range(1, len(n)):
				if n[i] == '0':
					continue
				else:
					break
		if i == 2:
			n = n[2:]
		elif len(d[y]) >= 2:
			a = d[y][-2]
			b = d[y][-1]
			n = n[:a] + n[a + 1:b] + n[b + 1:]
			ridofzero()
		else:
			n = n[i:]

def ridofzero():
	global n
	for i in range(len(n)):
		if n[i] == '0':
			continue
		else:
			break
	if len(n) > 0:
		n = n[i:]
n = str(input())
ch = False
if n[0] == '0':
	ch = True
	ridofzero()
d = {0: [], 1: [], 2: []}
s = 0
for i in range(len(n)):
	x = int(n[i])
	s += x
	d[x % 3].append(i)
if s < 3:
	if d[0] or ch:
		print(0)
	else:
		print(-1)
elif len(n) == 1:
	if not s % 3:
		print(n)
	else:
		print(-1)
else:
	x = s % 3
	if x == 0:
		pass
	elif x == 1:
		f(1, 2)
	else:
		f(2, 1)
	if len(n) == 0:
		print(-1)
	else:
		print(n)


import re
s = input()
t = s.translate(str.maketrans('0123456789', '0120120120'))
x = (t.count('1') + t.count('2') * 2) % 3
if x:
	res = ['']
	l = t.rsplit('12'[x == 2], 1)
	if len(l) == 2:
		a = len(l[0])
		res.append((s[:a], s[a + 1:]))
	l = t.rsplit('12'[x == 1], 2)
	if len(l) == 3:
		(a, b) = map(len, l[:2])
		res.append((s[:a], s[a + 1:a + b + 1], s[a + b + 2:]))
	s = max((re.sub('^0*(\\d)', '\\1', ''.join(e)) for e in res), key=len)
print(s or '-1')


import sys
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
inv = lambda x: pow(x, mod - 2, mod)
mod = 10 ** 9 + 7

def tot(n, rem, c):
	cnt = 0
	i = len(n) - 1
	s = n
	while i >= 0 and cnt < c:
		if int(s[i]) % 3 == rem:
			cnt += 1
			s = s[:i] + s[i + 1:]
		i -= 1
	if cnt != c:
		return ''
	while len(s) > 1 and s[0] == '0':
		s = s[1:]
	return s
n = ip()
a = 0
n1 = list(n)
for i in n1:
	a += int(i)
	a %= 3
if a == 0:
	print(n)
	exit(0)
x1 = tot(n, 3 - a, 2)
x2 = tot(n, a, 1)
if len(x1) == 0 and len(x2) == 0:
	print(-1)
elif len(x1) > len(x2):
	print(x1)
else:
	print(x2)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def main():
	a = [int(c) for c in S()]
	t = sum(a) % 3
	if t == 0:
		return ''.join(map(str, a))
	al = len(a)
	ds = []
	for i in range(al - 1, -1, -1):
		if a[i] % 3 == t:
			ds.append(i)
			if i != 0:
				break
			for j in range(1, al - 1):
				if a[j] != 0:
					break
				ds.append(j)
			break
	if not ds:
		ds = list(range(al))
	ds2 = []
	for i in range(al - 1, -1, -1):
		if a[i] * 2 % 3 == t:
			ds2.append(i)
			if len(ds2) < 2:
				continue
			if i != 0:
				break
			for j in range(1, al - 1):
				if j in ds2:
					continue
				if a[j] != 0:
					break
				ds2.append(j)
			break
	if len(ds2) < 2:
		ds2 = list(range(al))
	if len(ds) > len(ds2):
		ds = ds2
	if len(ds) == al:
		if 0 in a:
			return 0
		return -1
	return ''.join(map(str, [a[i] for i in range(al) if i not in ds]))
print(main())


n = list(input())
leng = 0
d = [0, 0, 0]
for x in n:
	leng += 1
	d[int(x) % 3] += 1
ratio = (d[1] + 2 * d[2]) % 3
if ratio == 0:
	print(''.join(n))
	exit()
if d[ratio] == 0:
	do = ratio ^ 3
	count = 2
elif d[ratio] == 1 and int(n[0]) % 3 == ratio and (n[1:3] == ['0', '0']):
	do = ratio ^ 3
	count = 2
	if d[do] == 0:
		do = ratio
		count = 1
else:
	do = ratio
	count = 1
index = leng - 1
if count >= leng:
	print(-1)
	exit()
for x in range(count):
	while int(n[index]) % 3 != do:
		index -= 1
	n[index] = ''
	index -= 1
index = 0
ans = ''.join(n)
while ans[index] == '0' and index < leng - count - 1:
	index += 1
print(ans[index:])


s = input().strip()
m = {0: 0, 1: 0, 2: 0}
bol = [0] * len(s)
two = []
one = []
i = 0
bc = 0
for a in s:
	m[int(a) % 3] = m[int(a) % 3] + 1
	if int(a) % 3 == 0:
		bol[i] = 1
		bc += 1
	elif int(a) % 3 == 1:
		one.append(i)
	elif int(a) % 3 == 2:
		two.append(i)
	i += 1
out = '' + s
i = 0
for a in s:
	if a != '0':
		break
	else:
		out = '' + out[i + 1:]
	i += 1
if (m[1] + 2 * m[2]) % 3 == 1:
	tm = ''
	tp = ''
	if m[1] >= 1:
		tm += s
		tm = tm[0:one[len(one) - 1]] + tm[one[len(one) - 1] + 1:]
		i = 0
		for a in tm:
			if a != '0':
				break
			else:
				tm = '' + tm[1:]
			i += 1
	if m[2] > 1:
		tp += s
		tp = tp[:two[len(two) - 1]] + tp[two[len(two) - 1] + 1:]
		tp = tp[:two[len(two) - 2]] + tp[two[len(two) - 2] + 1:]
		i = 0
		for a in tp:
			if a != '0':
				break
			else:
				tp = '' + tp[1:]
			i += 1
	if len(tp) > len(tm):
		out = tp
	else:
		out = tm
elif (m[1] + 2 * m[2]) % 3 == 2:
	tm = ''
	tp = ''
	if m[2] >= 1:
		tm += s
		tm = tm[:two[len(two) - 1]] + tm[two[len(two) - 1] + 1:]
		i = 0
		for a in tm:
			if a != '0':
				break
			else:
				tm = '' + tm[1:]
			i += 1
	if m[1] > 1:
		tp += s
		for z in range(len(one) - 1, len(one) - 3, -1):
			tp = tp[:one[z]] + tp[one[z] + 1:]
		i = 0
		for a in tp:
			if a != '0':
				break
			else:
				tp = '' + tp[1:]
			i += 1
	if len(tp) > len(tm):
		out = tp
	else:
		out = tm
if out == '' and m[0] > 0:
	out = '0'
elif out == '' and m[0] == 0:
	out = '-1'
print(out)


def func(t):
	an = ''
	l = len(t)
	for i in range(l):
		if t[i] != '0':
			break
	for j in range(i, l):
		an += t[j]
	return an
for nitish in range(1):
	s = input()
	ss = 0
	cnt = 10 ** 20
	cnt1 = 10 ** 20
	n = len(s)
	for i in range(n):
		ss += int(s[i])
	if ss % 3 == 0:
		print(s)
	elif ss % 3 == 1:
		ans = []
		for i in range(n - 1, -1, -1):
			if int(s[i]) % 3 == 1:
				ans.append(i)
				break
		ans1 = []
		for i in range(n - 1, -1, -1):
			if int(s[i]) % 3 == 2:
				ans1.append(i)
				if len(ans1) == 2:
					break
		if len(ans) == 0 and len(ans1) < 2:
			print(-1)
		else:
			cnt = 10 ** 20
			cnt1 = 10 ** 20
			t = ''
			tt = ''
			if len(ans) != 0:
				t = ''
				cnt = 1
				for i in range(n):
					if i != ans[0]:
						t += s[i]
				for i in t:
					if i == '0':
						cnt += 1
					else:
						break
			if len(ans1) == 2:
				cnt1 = 2
				tt = ''
				for i in range(n):
					if i not in ans1:
						tt += s[i]
				for i in tt:
					if i == '0':
						cnt1 += 1
					else:
						break
			if t != '' and cnt <= cnt1:
				an = func(t)
				if an != '':
					print(func(t))
				else:
					print(-1)
			elif tt != '':
				an = func(tt)
				if an != '':
					print(func(tt))
				else:
					print(-1)
			else:
				print(-1)
	else:
		ans = []
		for i in range(n - 1, -1, -1):
			if int(s[i]) % 3 == 2:
				ans.append(i)
				break
		ans1 = []
		for i in range(n - 1, -1, -1):
			if int(s[i]) % 3 == 1:
				ans1.append(i)
				if len(ans1) == 2:
					break
		if len(ans) == 0 and len(ans1) < 2:
			print(-1)
		else:
			cnt = 10 ** 20
			cnt2 = 10 ** 20
			t = ''
			tt = ''
			if len(ans) != 0:
				t = ''
				cnt = 1
				for i in range(n):
					if i != ans[0]:
						t += s[i]
				for i in t:
					if i == '0':
						cnt += 1
					else:
						break
			if len(ans1) == 2:
				cnt1 = 2
				tt = ''
				for i in range(n):
					if i not in ans1:
						tt += s[i]
				for i in tt:
					if i == '0':
						cnt1 += 1
					else:
						break
			if t != '' and cnt <= cnt1:
				an = func(t)
				if an != '':
					print(func(t))
				else:
					print(-1)
			elif tt != '':
				an = func(tt)
				if an != '':
					print(func(tt))
				else:
					print(-1)
			else:
				print(-1)


def f(n, r, k):
	tmp = n
	for t in range(k):
		for i in range(len(tmp) - 1, -1, -1):
			if int(tmp[i]) % 3 == r:
				tmp = tmp[:i] + tmp[i + 1:]
				break
	for i in range(len(tmp)):
		if int(tmp[i]) != 0:
			return ''.join(tmp[i:])
	if len(tmp) == 0 or 0 < int(tmp[len(tmp) - 1]):
		return ''
	else:
		return '0'
n = list(input())
cnt = {i: 0 for i in range(3)}
for d in n:
	cnt[int(d) % 3] += 1
r = (cnt[1] + 2 * cnt[2]) % 3
if r == 0:
	print(''.join(n))
else:
	ans = []
	if 0 < cnt[r]:
		ans.append(f(n, r, 1))
	if 1 < cnt[3 - r]:
		ans.append(f(n, 3 - r, 2))
	if len(ans) == 0 or len(''.join(ans)) == 0:
		print(-1)
	elif len(ans[-1]) <= len(ans[0]):
		print(ans[0])
	else:
		print(ans[-1])


n = input()
n = list(n[::-1])
(ans1, ans2, ans3, ans4) = ([], [], [], [])
s = 0
for i in n:
	s += int(i)
if s % 3 == 0:
	pass
else:
	flag1 = flag2 = flag3 = flag4 = False
	if s % 3 == 1:
		ans1 = n[:]
		for i in n:
			if int(i) % 3 == 1:
				ans1.remove(i)
				flag1 = True
				break
		ans2 = n[:]
		c = 0
		for i in n:
			if int(i) % 3 == 2:
				ans2.remove(i)
				c += 1
				if c == 2:
					flag2 = True
					break
	else:
		ans3 = n[:]
		for i in n:
			if int(i) % 3 == 2:
				ans3.remove(i)
				flag3 = True
				break
		ans4 = n[:]
		c = 0
		for i in n:
			if int(i) % 3 == 1:
				ans4.remove(i)
				c += 1
				if c == 2:
					flag4 = True
					break
	if len(ans1) != 0:
		ans1 = ''.join(ans1)
		if ans1.count('0') == len(ans1):
			ans1 = '0'
		else:
			ans1 = ans1[::-1].lstrip('0')
	else:
		ans1 = '-1'
	if len(ans2) != 0:
		ans2 = ''.join(ans2)
		if ans2.count('0') == len(ans2):
			ans2 = '0'
		else:
			ans2 = ans2[::-1].lstrip('0')
	else:
		ans2 = '-1'
	if len(ans3) != 0:
		ans3 = ''.join(ans3)
		if ans3.count('0') == len(ans3):
			ans3 = '0'
		else:
			ans3 = ans3[::-1].lstrip('0')
	else:
		ans3 = '-1'
	if len(ans4) != 0:
		ans4 = ''.join(ans4)
		if ans4.count('0') == len(ans4):
			ans4 = '0'
		else:
			ans4 = ans4[::-1].lstrip('0')
	else:
		ans4 = '-1'
n = ''.join(n[::-1])
if s % 3 == 0:
	print(n)
elif flag1 == True or flag2 == True or flag3 == True or (flag4 == True):
	if s % 3 == 1:
		if flag1 == flag2 == True:
			print(max(ans1, ans2, key=len))
		elif flag1 == True:
			print(ans1)
		elif flag2 == True:
			print(ans2)
	elif flag3 == flag4 == True:
		print(max(ans3, ans4, key=len))
	elif flag3 == True:
		print(ans3)
	elif flag4 == True:
		print(ans4)
else:
	print(-1)


s = input()
n = len(s)
(dp, leadingzero, camefrom, erasepos) = ([], 0, [], [])
for i in range(3):
	dp.append([int(1000000000.0)] * (n + 1))
	camefrom.append([int(-1)] * (n + 1))
dp[0][n] = 0
for i in range(n - 1, -1, -1):
	for j in range(3):
		dp[j][i] = min(dp[j][i], min(1 + dp[j][i + 1], dp[(int(j) - int(s[i])) % 3][i + 1]))
		if dp[j][i] >= int(1000000000.0):
			continue
		if 1 + dp[j][i + 1] == dp[j][i] and 1 + dp[j][i + 1] != dp[(int(j) - int(s[i])) % 3][i + 1]:
			camefrom[j][i] = j
		else:
			camefrom[j][i] = (int(j) - int(s[i])) % 3
ans = []
(parity, initialparity) = (0, 0)
for i in range(n - 1):
	previdx = camefrom[initialparity][i]
	if dp[initialparity][i] == 1 + dp[previdx][i + 1]:
		initialparity = previdx
		continue
	initialparity = previdx
	ans.append(s[i])
	parity += int(s[i]) % 3
if (parity + int(s[n - 1])) % 3 == 0:
	ans.append(s[n - 1])
for i in range(len(ans)):
	if ans[i] != '0':
		break
(ans1, ans2, leading1) = (ans, [s[0]], i)
(parity, initialparity) = (int(s[0]) % 3, -int(s[0]) % 3)
for i in range(1, n - 1):
	previdx = camefrom[initialparity][i]
	if previdx == -1:
		break
	if dp[initialparity][i] == 1 + dp[previdx][i + 1]:
		initialparity = previdx
		continue
	initialparity = previdx
	ans2.append(s[i])
	parity += int(s[i]) % 3
if n > 1 and (parity + int(s[n - 1])) % 3 == 0:
	ans2.append(s[n - 1])
if len(ans1) - leading1 < len(ans2) and parity % 3 == 0:
	print(''.join(ans2))
elif len(ans1) != 0:
	print(''.join(ans1[leading1:]))
else:
	print(-1)


def find1right(s, c, orig):
	r = s.rfind(c)
	if r != -1:
		res = orig[:r] + orig[r + 1:]
		res_strip = res.lstrip('0')
		ans = (len(orig) - len(res_strip), res_strip)
		if '0' in res and res_strip == '':
			ans = (len(s) - 1, '0')
		return ans
	return (len(orig), '')

def find2right(s, c, orig):
	r = s.rfind(c)
	if r != -1:
		rr = s.rfind(c, 0, r)
		if rr != -1:
			cur = orig[:rr] + orig[rr + 1:r] + orig[r + 1:]
			cur_strip = cur.lstrip('0')
			ans = (len(orig) - len(cur_strip), cur_strip)
			if '0' in cur and cur_strip == '':
				ans = (len(s) - 1, '0')
			return ans
	return (len(s), '')
s = input()
n = len(s)
rest = [int(c) % 3 for c in s]
rest_str = ''.join((str(i) for i in rest))
if len(s) == 1:
	if int(s) % 3 == 0:
		print(s)
	else:
		print(-1)
	exit()
if s[1:] == '0' * (n - 1):
	print(0)
	exit()
q = sum(rest) % 3
if q == 0:
	ans = s
elif q == 1:
	ans = (n, '')
	ans = min(ans, find1right(rest_str, '1', s))
	ans = min(ans, find2right(rest_str, '2', s))
	ans = ans[1]
else:
	ans = (n, '')
	ans = min(ans, find1right(rest_str, '2', s))
	ans = min(ans, find2right(rest_str, '1', s))
	ans = ans[1]
print(ans if ans != '' else -1)


import re
s = input()
x = sum(map(ord, s)) % 3
if x:
	(t, l) = (s.translate(str.maketrans('4578', '1212')), [''])
	i = t.rfind('012'[x])
	if i > -1:
		l.append((s[:i], s[i + 1:]))
	i = t.rfind('021'[x])
	if i > -1:
		j = t.rfind('021'[x], 0, i)
		if j > -1:
			l.append((s[:j], s[j + 1:i], s[i + 1:]))
	s = max((re.sub('^0*(?=.)', '', ''.join(e)) for e in l), key=len)
print(s or '-1')


import sys
s = list(input())
digsum = 0
rem = [0 for i in range(len(s))]
for i in range(0, len(s)):
	dig = ord(s[i]) - ord('0')
	digsum += dig
	rem[i] = dig % 3
currem = digsum % 3
if currem == 0:
	print(''.join(s))
	sys.exit()
if len(s) == 1:
	print(-1)
	sys.exit()
inds1 = []
inds2 = []
for i in range(len(rem) - 1, -1, -1):
	if rem[i] == 2 and len(inds2) < 2:
		inds2.append(i)
	if rem[i] == 1 and len(inds1) < 2:
		inds1.append(i)
vs = []
if currem == 1:
	if len(inds1) >= 1:
		vs.append(s[:inds1[0]] + s[inds1[0] + 1:])
	inds2.sort()
	if len(inds2) == 2:
		vs.append(s[:inds2[0]] + s[inds2[0] + 1:inds2[1]] + s[inds2[1] + 1:])
if currem == 2:
	if len(inds2) >= 1:
		vs.append(s[:inds2[0]] + s[inds2[0] + 1:])
	inds1.sort()
	if len(inds1) == 2:
		vs.append(s[:inds1[0]] + s[inds1[0] + 1:inds1[1]] + s[inds1[1] + 1:])
ans = -1
anslen = 0
for i in range(len(vs)):
	ln = len(vs[i])
	for j in range(len(vs[i])):
		if vs[i][j] != '0' or j == len(vs[i]) - 1:
			break
		ln -= 1
	vs[i] = vs[i][-ln:]
	if ln != 0 and (ans == -1 or anslen < ln):
		anslen = ln
		ans = i
if ans == -1:
	print(-1)
else:
	print(''.join(vs[ans]))


s = input()
rem = 0
rems = [0] * len(s)
one = []
two = []
for i in range(len(s)):
	rems[i] = int(s[i]) % 3
	if rems[i] == 1:
		one.append(i)
	elif rems[i] == 2:
		two.append(i)
	rem += int(s[i])
	rem %= 3
include = [1] * len(s)
include1 = [1] * len(s)
f = 0
if rem == 1:
	if len(one):
		include[one[len(one) - 1]] = 0
	if len(two) >= 2:
		include1[two[len(two) - 1]] = 0
		include1[two[len(two) - 2]] = 0
elif rem == 2:
	(one, two) = (two, one)
	if len(one):
		include[one[len(one) - 1]] = 0
	if len(two) >= 2:
		include1[two[len(two) - 1]] = 0
		include1[two[len(two) - 2]] = 0
ans = []
ans1 = []
for i in range(len(s)):
	if include[i]:
		ans.append(int(s[i]))
	if include1[i]:
		ans1.append(int(s[i]))
fans = []
fans1 = []
for i in ans:
	if f == 0 and i == 0:
		continue
	if i:
		f = 1
		fans.append(i)
	elif f:
		fans.append(i)
ans.sort()
if len(ans):
	if ans[0] == ans[len(ans) - 1] == 0:
		fans.append(0)
f = 0
for i in ans1:
	if f == 0 and i == 0:
		continue
	if i:
		f = 1
		fans1.append(i)
	elif f:
		fans1.append(i)
ans1.sort()
if len(ans1):
	if ans1[0] == ans1[len(ans1) - 1] == 0:
		fans1.append(0)
f = 0
if (len(fans) == 0 or sum(fans) % 3) and (len(fans1) == 0 or sum(fans1) % 3):
	print(-1)
else:
	if len(fans) < len(fans1):
		(fans, fans1) = (fans1, fans)
	if sum(fans) % 3 == 0:
		for i in fans:
			print(i, end='')
	else:
		for i in fans1:
			print(i, end='')


import sys
a = list(map(int, list(input())))
n = len(a)
su = sum(a)
l1 = [i for i in range(n) if a[i] % 3 == 1]
l2 = [i for i in range(n) if a[i] % 3 == 2]

def erase_leading_zeroes(li):
	i = 0
	while i < n:
		if len(li) <= 1:
			return
		if li[i] == 0:
			li.pop(i)
		else:
			return
if su % 3 == 1:
	tmp1 = a[:]
	tmp2 = a[:]
	if len(l1) >= 1:
		tmp1.pop(l1[-1])
		erase_leading_zeroes(tmp1)
	if len(l2) >= 2:
		tmp2.pop(l2[-1])
		tmp2.pop(l2[-2])
		erase_leading_zeroes(tmp2)
	if sum(tmp1) % 3 == 0 or sum(tmp2) % 3 == 0:
		if sum(tmp1) % 3 == 0:
			a = tmp1
		if sum(tmp2) % 3 == 0 and (sum(tmp1) % 3 != 0 or len(tmp2) > len(tmp1)):
			a = tmp2
	else:
		print(-1)
		sys.exit()
elif su % 3 == 2:
	tmp1 = a[:]
	tmp2 = a[:]
	if len(l2) >= 1:
		tmp1.pop(l2[-1])
		erase_leading_zeroes(tmp1)
	if len(l1) >= 2:
		tmp2.pop(l1[-1])
		tmp2.pop(l1[-2])
		erase_leading_zeroes(tmp2)
	if sum(tmp1) % 3 == 0 or sum(tmp2) % 3 == 0:
		if sum(tmp1) % 3 == 0:
			a = tmp1
		if sum(tmp2) % 3 == 0 and (sum(tmp1) % 3 != 0 or len(tmp2) > len(tmp1)):
			a = tmp2
	else:
		print(-1)
		sys.exit()
if len(a) >= 1:
	print(''.join(map(str, a)))
else:
	print(-1)


digits = [int(d) for d in input()]
remainder = sum(digits) % 3

def clip(digits):
	digits_copy = digits.copy()
	while len(digits_copy) > 0:
		if digits_copy[0] == 0 and len(digits_copy) > 1:
			del digits_copy[0]
		else:
			break
	if len(digits_copy) != 0:
		return ''.join([str(c) for c in digits_copy])
ans = None
if remainder == 0:
	ans = clip(digits)
elif remainder == 1:
	length = len(digits)
	broke = 0
	digits_del1 = digits.copy()
	digits_del2 = digits.copy()
	(length1, length2) = (None, None)
	for i in range(length):
		if digits_del1[length - 1 - i] % 3 == 1:
			del digits_del1[length - 1 - i]
			broke += 1
			break
	if broke != 0:
		ans = clip(digits_del1)
		length1 = None if ans is None else len(ans)
	count = 0
	mod2_index = []
	for i in range(length):
		if digits[length - 1 - i] % 3 == 2:
			count += 1
			mod2_index.append(length - 1 - i)
			if count == 2:
				break
	if count == 2:
		del digits_del2[mod2_index[0]]
		del digits_del2[mod2_index[1]]
		ans2 = clip(digits_del2)
		length2 = None if ans2 is None else len(ans2)
		if length1 is None:
			if length2 is not None:
				ans = ans2
		elif length2 > length1:
			ans = ans2
elif remainder == 2:
	length = len(digits)
	broke = 0
	digits_del1 = digits.copy()
	digits_del2 = digits.copy()
	(length1, length2) = (None, None)
	for i in range(length):
		if digits_del1[length - 1 - i] % 3 == 2:
			del digits_del1[length - 1 - i]
			broke += 1
			break
	if broke != 0:
		ans = clip(digits_del1)
		length1 = None if ans is None else len(ans)
	count = 0
	mod2_index = []
	for i in range(length):
		if digits[length - 1 - i] % 3 == 1:
			count += 1
			mod2_index.append(length - 1 - i)
			if count == 2:
				break
	if count == 2:
		del digits_del2[mod2_index[0]]
		del digits_del2[mod2_index[1]]
		ans2 = clip(digits_del2)
		length2 = None if ans2 is None else len(ans2)
		if length1 is None:
			if length2 is not None:
				ans = ans2
		elif length2 > length1:
			ans = ans2
if ans is None:
	print(-1)
else:
	print(ans)