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.

Source: codeforces | Difficulty: HARD
Tags: Number theory

Dynamic programming

Mathematics

Greedy algorithms


Solutions:
a = input()
if len(a) == 1:
	if int(a) % 3 == 0:
		print(a)
	else:
		print(-1)
	exit(0)
one = []
two = []
sum = 0
(zs, zf) = (0, 0)
for i in range(len(a)):
	q = int(a[i])
	sum += q
	if q == 0:
		if zs == 0:
			zs = i
	elif zs != 0 and zf == 0:
		zf = i
	if q % 3 == 1:
		if len(one) <= 4:
			one.append(i)
	elif q % 3 == 2:
		if len(two) <= 4:
			two.append(i)
if zf == 0 and zs != 0:
	zf = len(a) - 1
sol = []
s = ''
if sum % 3 == 1:
	if len(one) > 0:
		s = a[:one[-1]] + a[one[-1] + 1:]
		sol.append(s)
	if len(a) > 1 and len(two) > 1:
		s = a[:two[-2]] + a[two[-2] + 1:two[-1]] + a[two[-1] + 1:]
		sol.append(s)
if sum % 3 == 2:
	if len(two) > 0:
		s = a[:two[-1]] + a[two[-1] + 1:]
		sol.append(s)
	if len(a) > 1 and len(one) > 1:
		s = a[:one[-2]] + a[one[-2] + 1:one[-1]] + a[one[-1] + 1:]
		sol.append(s)
if sum % 3 == 0:
	print(a)
else:
	mini = 0
	max = 0
	for i in range(len(sol)):
		if sol[i] == '':
			sol[i] = '-1'
		elif sol[i][0] == '0':
			sol[i] = sol[i][zf - zs:]
			if len(sol[i]) > 0:
				if sol[i][0] == '0':
					f = -1
					for j in range(len(sol[i])):
						if sol[i][j] != '0':
							f = j
							break
					if f != -1:
						sol[i] = sol[i][f:]
					else:
						sol[i] = '0'
			if len(sol[i]) == 0:
				sol[i] = '0'
		if len(sol[i]) > max:
			max = len(sol[i])
			mini = i
	print(sol[mini])


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()


s = list(input())

def db3(n, sm):
	new = n.copy()
	sm %= 3
	if sm == 0:
		return ''.join(new)
	else:
		if sm == 2:
			for j in range(1, len(new)):
				if int(new[j]) % 3 == 2:
					new[j] = ''
					return ''.join(new)
			for j in range(1, len(new)):
				if int(new[j]) % 3 == 1:
					new[j] = ''
					sm -= 1
				if sm == 0:
					return ''.join(new)
		else:
			for j in range(1, len(new)):
				if int(new[j]) % 3 == 1:
					new[j] = ''
					return ''.join(new)
			sm = 2
			for j in range(1, len(new)):
				if int(new[j]) % 3 == 2:
					new[j] = ''
					sm -= 1
				if sm == 0:
					return ''.join(new)
		if '0' in new:
			return '0'
		else:
			return ''
a = 3
i = 0
l = []
Sm = sum([int(x) for x in s])
val = 0
while a and i < len(s):
	if s[i] != '0':
		l.append(db3(s[i:], Sm - val))
		a -= 1
		val += int(s[i])
	i += 1
ans = max(l, key=len)
if ans:
	print(ans)
else:
	print(-1)


import re
f = lambda t: re.sub('^0+(\\d)', '\\1', t)
g = lambda k: n - p.index(s, k)
t = input()
n = len(t) - 1
p = [int(q) % 3 for q in t][::-1]
s = sum(p) % 3
if s == 0:
	print(t)
	exit()
u = v = ''
if s in p:
	i = g(0)
	u = f(t[:i] + t[i + 1:])
s = 3 - s
if p.count(s) > 1:
	i = g(0)
	j = g(n - i + 1)
	v = f(t[:j] + t[j + 1:i] + t[i + 1:])
t = u if len(u) > len(v) else v
print(t if t else -1)


import sys
from itertools import compress
s = input()
n = len(s)
mod = [0] * n
for (i, x) in enumerate(map(int, s)):
	mod[i] = x % 3
total_mod = sum(mod) % 3

def remove_zeros(a):
	for i in range(n):
		if not a[i]:
			continue
		if s[i] == '0':
			a[i] = 0
		else:
			return
if total_mod == 0:
	a = [1] * n
	remove_zeros(a)
	ans = ''.join(compress(s, a))
	if ans:
		print(ans)
	else:
		print(0 if '0' in s else -1)
else:
	(ans1, ans2) = ('', '')
	for i in range(n - 1, -1, -1):
		if mod[i] == total_mod:
			a = [1] * n
			a[i] = 0
			remove_zeros(a)
			ans1 = ''.join(compress(s, a))
			break
	rem = 2
	a = [1] * n
	for i in range(n - 1, -1, -1):
		if mod[i] == 3 - total_mod:
			a[i] = 0
			rem -= 1
			if rem == 0:
				remove_zeros(a)
				ans2 = ''.join(compress(s, a))
				break
	ans = ans1 if len(ans1) > len(ans2) else ans2
	if ans:
		print(ans)
	else:
		print(0 if '0' in s else -1)


import sys, math, queue
sys.setrecursionlimit(1000000)

def removeZeros(x):
	i = 0
	while i < len(x) and x[i] == 0:
		i += 1
	return x[i:]
n = input()
d = list(map(int, list(n)))
s = sum(d)
if s % 3 == 0:
	print(n)
	exit()
r = [[], [], []]
for i in range(len(d)):
	r[d[i] % 3].append(i)
if s % 3 == 1:
	a1 = []
	a2 = []
	if len(r[1]) >= 1:
		for i in range(len(d)):
			if i != r[1][-1]:
				a1.append(d[i])
	a1 = removeZeros(a1)
	if len(r[2]) >= 2:
		for i in range(len(d)):
			if i != r[2][-1] and i != r[2][-2]:
				a2.append(d[i])
	a2 = removeZeros(a2)
	if len(a1) == 0 and len(a2) == 0:
		for i in range(len(d)):
			if d[i] == 0:
				print(0)
				exit()
		print(-1)
		exit()
	elif len(a1) > len(a2):
		for i in range(len(a1)):
			print(a1[i], end='')
		exit()
	else:
		for i in range(len(a2)):
			print(a2[i], end='')
		exit()
else:
	a1 = []
	a2 = []
	if len(r[2]) >= 1:
		for i in range(len(d)):
			if i != r[2][-1]:
				a1.append(d[i])
	a1 = removeZeros(a1)
	if len(r[1]) >= 2:
		for i in range(len(d)):
			if i != r[1][-1] and i != r[1][-2]:
				a2.append(d[i])
	a2 = removeZeros(a2)
	if len(a1) == 0 and len(a2) == 0:
		for i in range(len(d)):
			if d[i] == 0:
				print(0)
				exit()
		print(-1)
		exit()
	elif len(a1) > len(a2):
		for i in range(len(a1)):
			print(a1[i], end='')
		exit()
	else:
		for i in range(len(a2)):
			print(a2[i], end='')
		exit()


def find(s):
	if len(s) == 1:
		if int(s) % 3 == 0:
			return s
		return -1
	digit = [[] for i in range(3)]
	zeros = []
	for i in range(len(s)):
		digit[int(s[i]) % 3] += [i]
		if s[i] == '0':
			zeros += [i]
	total = (len(digit[1]) * 1 + len(digit[2]) * 2) % 3
	if total == 0:
		return s
	if total == 1:
		s1 = ''
		if len(digit[1]) >= 1:
			p1 = digit[1][-1]
			s1 = s[:p1] + s[p1 + 1:]
			last_pos = len(s1)
			for i in range(len(s1)):
				if s1[i] != '0':
					last_pos = i
					break
			if last_pos < len(s1):
				s1 = s1[last_pos:]
			elif len(s1) > 0:
				if s1[-1] == '0':
					s1 = '0'
		s2 = ''
		if len(digit[2]) >= 2:
			p1 = digit[2][-2]
			p2 = digit[2][-1]
			s2 = s[:p1] + s[p1 + 1:p2] + s[p2 + 1:]
			last_pos = len(s2)
			for i in range(len(s2)):
				if s2[i] != '0':
					last_pos = i
					break
			if last_pos < len(s2):
				s2 = s2[last_pos:]
			elif len(s2) > 0:
				if s2[-1] == '0':
					s2 = '0'
		if s1 == '' and s2 == '':
			return -1
		elif s1 == '':
			return s2
		elif s2 == '':
			return s1
		else:
			if len(s1) > len(s2):
				return s1
			return s2
	if total == 2:
		(digit[1], digit[2]) = (digit[2], digit[1])
		s1 = ''
		if len(digit[1]) >= 1:
			p1 = digit[1][-1]
			s1 = s[:p1] + s[p1 + 1:]
			last_pos = len(s1)
			for i in range(len(s1)):
				if s1[i] != '0':
					last_pos = i
					break
			if last_pos < len(s1):
				s1 = s1[last_pos:]
			elif len(s1) > 0:
				if s1[-1] == '0':
					s1 = '0'
		s2 = ''
		if len(digit[2]) >= 2:
			p1 = digit[2][-2]
			p2 = digit[2][-1]
			s2 = s[:p1] + s[p1 + 1:p2] + s[p2 + 1:]
			last_pos = len(s2)
			for i in range(len(s2)):
				if s2[i] != '0':
					last_pos = i
					break
			if last_pos < len(s2):
				s2 = s2[last_pos:]
			elif len(s2) > 0:
				if s2[-1] == '0':
					s2 = '0'
		if s1 == '' and s2 == '':
			return -1
		elif s1 == '':
			return s2
		elif s2 == '':
			return s1
		else:
			if len(s1) > len(s2):
				return s1
			return s2
print(find(input()))


def lencheck(item1, item2):
	if len(item1) > len(item2):
		return True

def leadingzero(string):
	while string[0:1] == '0' and len(string) != 1:
		string = string[1:]
	return string
import sys
data = sys.stdin.read().strip()
sumOfDigits = 0
for i in data:
	sumOfDigits += int(i)
data2 = ''
if sumOfDigits % 3 == 0:
	data2 = leadingzero(data)
count = 0
record = -1
if sumOfDigits % 3 == 1:
	for i in data[0:]:
		if int(i) % 3 == 1:
			record = count
		count += 1
	if record != -1:
		if lencheck(leadingzero(data[0:record] + data[record + 1:]), data2):
			data2 = leadingzero(data[0:record] + data[record + 1:])
	if len(data2) < len(data) - 1:
		for i in range(0, len(data)):
			if int(data[i]) % 3 == 2:
				for j in range(i + 1, len(data)):
					if int(data[j]) % 3 == 2 and lencheck(leadingzero(data[0:i] + data[i + 1:j] + data[j + 1:]), data2):
						data2 = leadingzero(data[0:i] + data[i + 1:j] + data[j + 1:])
						record = j
	if len(data) > 2 and int(data[2:]) % 3 == 0 and lencheck(leadingzero(data[2:]), data2):
		data2 = leadingzero(data[2:])
	elif len(data) > 1 and int(data[1:]) % 3 == 0 and lencheck(leadingzero(data[1:]), data2):
		data2 = leadingzero(data[1:])
if sumOfDigits % 3 == 2:
	for i in data[0:]:
		if int(i) % 3 == 2:
			record = count
		count += 1
	if record != -1 and lencheck(leadingzero(data[0:record] + data[record + 1:]), data2):
		data2 = leadingzero(data[0:record] + data[record + 1:])
	if len(data2) < len(data) - 1:
		for i in range(0, len(data)):
			if int(data[i]) % 3 == 1:
				for j in range(i + 1, len(data)):
					if int(data[j]) % 3 == 1 and lencheck(leadingzero(data[0:i] + data[i + 1:j] + data[j + 1:]), data2):
						data2 = leadingzero(data[0:i] + data[i + 1:j] + data[j + 1:])
						record = j
	if len(data) > 2 and int(data[2:]) % 3 == 0 and lencheck(leadingzero(data[2:]), data2):
		data2 = leadingzero(data[2:])
	elif len(data) > 1 and int(data[1:]) % 3 == 0 and lencheck(leadingzero(data[1:]), data2):
		data2 = leadingzero(data[1:])
if data2 == '':
	print(-1)
if data2 != '':
	print(data2)


n = list(map(int, list(input())))
mds = []
for i in n:
	mds.append(i % 3)
mod = sum(mds) % 3
try:
	if mod == 1:
		i = len(mds) - 1
		nn = n[:]
		while i >= 0:
			if mds[i] == 1:
				del nn[i]
				break
			i -= 1
		i = len(mds) - 1
		count = []
		nnn = n[:]
		while i >= 0:
			if mds[i] == 2:
				count.append(i)
			if len(count) == 2:
				del nnn[count[0]]
				del nnn[count[1]]
				break
			i -= 1
		if len(nn) != 1 and nn[0] == 0:
			while nn[0] == 0:
				del nn[0]
		if len(nnn) != 1 and nnn[0] == 0:
			while nnn[0] == 0:
				del nnn[0]
		if len(nn) < len(n) and len(nn) > len(nnn) or (len(nnn) == len(n) and len(nn) < len(n)):
			n = nn[:]
		elif len(nnn) < len(n):
			n = nnn[:]
		else:
			n = [-1]
	if mod == 2:
		i = len(mds) - 1
		nn = n[:]
		while i >= 0:
			if mds[i] == 2:
				del nn[i]
				break
			i -= 1
		i = len(mds) - 1
		count = []
		nnn = n[:]
		while i >= 0:
			if mds[i] == 1:
				count.append(i)
			if len(count) == 2:
				del nnn[count[0]]
				del nnn[count[1]]
				break
			i -= 1
		if len(nn) != 1 and nn[0] == 0:
			while nn[0] == 0:
				del nn[0]
		if len(nnn) != 1 and nnn[0] == 0:
			while nnn[0] == 0:
				del nnn[0]
		if len(nn) < len(n) and len(nn) > len(nnn) or (len(nnn) == len(n) and len(nn) < len(n)):
			n = nn[:]
		elif len(nnn) < len(n):
			n = nnn[:]
		else:
			n = [-1]
	if len(n) != 1 and n[0] == 0:
		while n[0] == 0:
			del n[0]
	if len(n) == 0:
		n = [-1]
except:
	if 0 in n:
		n = [0]
	else:
		n = [-1]
print(''.join(map(str, n)))


def sumstr(s):
	sums = 0
	for i in range(len(s)):
		sums += int(s[i])
	return sums

def mod3(s):
	sums = sumstr(s)
	return sums % 3

def onlyzeros(s):
	i = 0
	while i < len(s):
		if s[i] != '0':
			return False
		i += 1
	return True

def lastin(s, s1):
	maxf = -1
	for d in s1:
		f = s.rfind(d)
		if f > maxf:
			maxf = f
	return maxf

def del1(s, s1):
	if s[0] == '-':
		return '-'
	maxf = lastin(s, s1)
	if maxf != -1:
		if maxf != 0:
			ret = s[:maxf]
			if maxf + 1 < len(s):
				ret = ret + s[maxf + 1:]
			return ret
		elif len(s) > 1 and s[1] != '0':
			return s[1:]
		elif len(s) == 1:
			return '-'
		else:
			ind = 1
			while ind < len(s) and s[ind] == '0':
				ind += 1
			if ind == len(s):
				return '0'
			else:
				return s[ind:]
	else:
		return '-'

def only0(s):
	if '0' in s:
		return '0'
	else:
		return '-'
from random import randint

def testing():
	s = input()
	m3 = mod3(s)
	if m3 == 0:
		print(s)
	elif m3 == 1:
		var1 = del1(s, '147')
		var2 = del1(del1(s, '258'), '258')
		var3 = only0(s)
		l1 = len(var1)
		l2 = len(var2)
		if l1 > l2:
			print(var1)
		elif l2 > l1:
			print(var2)
		elif var1[0] != '-':
			print(var1)
		elif var2[0] != '-':
			print(var2)
		elif var3[0] != '-':
			print(var3)
		else:
			print(-1)
	elif m3 == 2:
		var1 = del1(s, '258')
		var2 = del1(del1(s, '147'), '147')
		var3 = only0(s)
		l1 = len(var1)
		l2 = len(var2)
		if l1 > l2:
			print(var1)
		elif l2 > l1:
			print(var2)
		elif var1[0] != '-':
			print(var1)
		elif var2[0] != '-':
			print(var2)
		elif var3[0] != '-':
			print(var3)
		else:
			print(-1)
for i in range(1):
	testing()


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 n_by_deleting_r(n, r):
	if all(map(lambda x: int(x) % 3 != r, n)):
		return ''
	for idx in range(len(n) - 1, -1, -1):
		if r == int(n[idx]) % 3:
			return n[:idx] + n[idx + 1:]

def n_by_two_digit_remove(n, r):
	if sum(map(lambda x: int(x) % 3 == r, n)) < 2:
		return ''
	L = -1
	R = -1
	for i in range(len(n) - 1, -1, -1):
		if int(n[i]) % 3 == r:
			if R == -1:
				R = i
			else:
				L = i
				break
	return n[:L] + n[L + 1:R] + n[R + 1:]

def rem_prefix0(s: str):
	for i in range(len(s)):
		if s[i] != '0':
			return s[i:]
	return '0'

def solve(n):
	s = sum(map(int, list(n)))
	r = s % 3
	if r == 0:
		return n
	sol1 = n_by_deleting_r(n, r)
	sol2 = n_by_two_digit_remove(n, 3 - r)
	if sol1 == '' and sol2 == '':
		return '-1'
	if sol1 == '':
		return rem_prefix0(sol2)
	if sol2 == '':
		return rem_prefix0(sol1)
	if len(rem_prefix0(sol1)) > len(rem_prefix0(sol2)):
		return rem_prefix0(sol1)
	else:
		return rem_prefix0(sol2)

def main():
	print(solve(input()))
main()


m = list(input())
n = list(map(int, m))
s = sum(n)
l = len(n)
if s % 3 == 0:
	print(''.join(m))
else:
	r = s % 3
	for j in range(1, len(n)):
		if n[j] % 3 == r:
			n.pop(j)
			break
	if len(n) == l:
		flag = 0
		if n[0] % 3 == r:
			if len(n) > 1:
				if n[1] != 0:
					n.pop(0)
					flag = 1
		if flag == 0:
			num = []
			count = 0
			for j in range(1, len(n)):
				if n[j] % 3 != 0 and n[j] % 3 != r:
					count += 1
					num.append(j)
					if count == 2:
						break
			if count == 2:
				n.pop(num[1])
				n.pop(num[0])
			elif count == 1:
				n.pop(num[0])
				n.pop(0)
			else:
				n.pop(0)
	if len(n) == 0:
		print(-1)
	else:
		j = 0
		flag = 0
		while flag == 0 and len(n) > 1:
			flag = 1
			if n[j] == 0:
				n.pop(j)
				flag = 0
		for j in range(len(n)):
			print(n[j], end='')
		print('')


def erase(n, rem, count):
	cnt = 0
	i = len(n) - 1
	s = n
	while i >= 0 and cnt < count:
		if int(s[i]) % 3 == rem:
			cnt += 1
			s = s[:i] + s[i + 1:]
		i -= 1
	if cnt != count:
		return ''
	while len(s) > 1 and s[0] == '0':
		s = s[1:]
	return s
n = input()
a = 0
for x in range(len(n)):
	a += int(n[x])
	a %= 3
if a == 0:
	print(n)
else:
	n1 = erase(n, a, 1)
	n2 = erase(n, 3 - a, 2)
	if len(n1) == 0 and len(n2) == 0:
		print(-1)
	elif len(n1) > len(n2):
		print(n1)
	else:
		print(n2)


def erase(s, n, m):
	p = '147' if n == 1 else '258'
	if sum((s.count(c) for c in p)) < m:
		return []
	t = list(reversed(s))
	for c in p:
		while t.count(c) > 0 and m > 0:
			t.remove(c)
			m -= 1
	while len(t) > 1 and t[-1] == '0':
		t.pop()
	return list(reversed(t))

def solve(s):
	n = sum((int(c) for c in s)) % 3
	if n == 0:
		a = b = s
	if n == 1:
		a = erase(s, 1, 1)
		b = erase(s, 2, 2)
	if n == 2:
		a = erase(s, 2, 1)
		b = erase(s, 1, 2)
	s = max(a, b, key=len)
	return '-1' if s == [] else s

def divide_by_three(s):
	return solve(s)
s = list(input())
result = divide_by_three(s)
print(*result, sep='')


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')


def RemoveFirstZero(str):
	count = 0
	for i in range(len(str)):
		if str[i] != '0':
			count = i
			break
	return str[count:]

def RemainderOf3(str):
	sum = 0
	for i in range(len(str)):
		sum += int(str[i])
	return sum % 3
input1 = input()
setofans = []
t = 0
p = True
q = True
max = 0
if RemainderOf3(input1) == 0:
	setofans.append(input1)
elif RemainderOf3(input1) == 1:
	for i in range(len(input1) - 1, -1, -1):
		if int(input1[i]) % 3 == 1:
			if p:
				setofans.append(input1[:i] + input1[i + 1:])
				p = False
			else:
				continue
		if int(input1[i]) % 3 == 2:
			if q:
				max += 1
				if max == 1:
					t = i
				elif max == 2:
					setofans.append(input1[:i] + input1[i + 1:t] + input1[t + 1:])
					q = False
				else:
					continue
			else:
				continue
else:
	for i in range(len(input1) - 1, -1, -1):
		if int(input1[i]) % 3 == 1:
			if q:
				max += 1
				if max == 1:
					t = i
				elif max == 2:
					setofans.append(input1[:i] + input1[i + 1:t] + input1[t + 1:])
					q = False
				else:
					continue
			else:
				continue
		if int(input1[i]) % 3 == 2:
			if p:
				setofans.append(input1[:i] + input1[i + 1:])
				p = False
			else:
				continue
ans = list(map(RemoveFirstZero, setofans))
max = len(ans[0])
a = ans[0]
for i in ans:
	if max < len(i):
		max = len(i)
		a = i
if a != '':
	if a[0] != '0':
		print(a)
	else:
		print('0')
else:
	print(-1)


n = input()
a = [0] * len(n)
b1 = [0] * len(n)
b2 = [0] * len(n)
k = 0
c1 = 0
c2 = 0
ans = ''
for i in range(len(n)):
	a[i] = int(n[i])
	b1[i] = int(n[i])
	b2[i] = int(n[i])
	k += a[i]
	k %= 3
if k != 0:
	k1 = k
	for i in range(len(b1) - 1, -1, -1):
		if b1[i] == 1 or b1[i] == 4 or b1[i] == 7:
			del b1[i]
			c1 += 1
			k1 -= 1
			if k1 % 3 == 0:
				if len(a) != 0:
					for i in range(len(n)):
						if len(b1) > 1:
							if b1[0] == 0:
								c1 += 1
								del b1[0]
							else:
								break
				break
	if c1 == 0:
		k1 = k
		for i in range(len(b1) - 1, -1, -1):
			if b1[i] == 5 or b1[i] == 2 or b1[i] == 8:
				del b1[i]
				c1 += 1
				k1 -= 2
				if k1 % 3 == 0:
					if len(b1) != 0:
						for i in range(len(n)):
							if len(b1) > 1:
								if b1[0] == 0:
									c1 += 1
									del b1[0]
								else:
									break
					break
	k2 = k
	for i in range(len(b2) - 1, -1, -1):
		if b2[i] == 2 or b2[i] == 5 or b2[i] == 8:
			del b2[i]
			c2 += 1
			k2 -= 2
			if k2 % 3 == 0:
				if len(b2) != 0:
					for i in range(len(n)):
						if len(b2) > 1:
							if b2[0] == 0:
								c2 += 1
								del b2[0]
							else:
								break
				break
	if c2 == 0:
		k2 = k
		for i in range(len(b2) - 1, -1, -1):
			if b2[i] == 1 or b2[i] == 4 or b2[i] == 7:
				del b2[i]
				c2 += 1
				k2 -= 1
				if k2 % 3 == 0:
					if len(b2) != 0:
						for i in range(len(n)):
							if len(b2) > 1:
								if b2[0] == 0:
									c2 += 1
									del b2[0]
								else:
									break
					break
	if len(b2) >= len(b1):
		if k2 % 3 == 0:
			a = b2
		elif k1 % 3 == 0:
			a = b1
		else:
			a = []
	elif k1 % 3 == 0:
		a = b1
	elif k2 % 3 == 0:
		a = b2
	else:
		a = []
if len(a) == 0:
	print('-1')
else:
	for i in range(len(a)):
		ans += str(a[i])
	print(ans)


import sys
n = list(input(''))
l = [int(c) % 3 for c in n][::-1]
d = sum(l) % 3
if len(n) == 1:
	if n == ['3']:
		print(3)
	else:
		print(-1)
	sys.exit(0)

def li(x):
	global n
	del n[len(n) - 1 - l.index(x)]
	del l[l.index(x)]

def cond(x):
	x = x.lstrip('0')
	if len(x) == 0:
		return '0'
	return x
if d == 0:
	pass
elif d in l:
	if l.index(d) == len(l) - 1 and int(n[1]) == 0 and (l.count(3 - d) >= 2):
		li(3 - d)
		li(3 - d)
	else:
		li(d)
else:
	li(3 - d)
	li(3 - d)
if len(n) > 0:
	q = cond(''.join(n))
	print(q)
else:
	print(-1)


def deleteD(n, num):
	rel = list(reversed(s))
	for digit in reversed(s):
		if int(digit) % 3 == n:
			rel.remove(digit)
			num -= 1
		if num == 0:
			if len(rel) == 0:
				return []
			while rel[-1] == '0' and len(rel) > 1:
				rel.pop()
			return list(reversed(rel))
	return []
s = input()
n = sum([int(c) for c in s]) % 3
if n == 0:
	print(s)
else:
	if n == 1:
		a = deleteD(1, 1)
		b = deleteD(2, 2)
	else:
		a = deleteD(1, 2)
		b = deleteD(2, 1)
	s = max(a, b, key=len)
	if len(s):
		print(''.join(s))
	else:
		print(-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 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

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			prime.append(p)
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	return prime

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return fac

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x

def deleteD(n, num):
	rel = list(reversed(s))
	for digit in reversed(s):
		if int(digit) % 3 == n:
			rel.remove(digit)
			num -= 1
		if num == 0:
			if len(rel) == 0:
				return []
			while rel[-1] == '0' and len(rel) > 1:
				rel.pop()
			return list(reversed(rel))
	return []
s = input()
n = sum([int(c) for c in s]) % 3
if n == 0:
	print(s)
else:
	if n == 1:
		a = deleteD(1, 1)
		b = deleteD(2, 2)
	else:
		a = deleteD(1, 2)
		b = deleteD(2, 1)
	s = max(a, b, key=len)
	if len(s):
		print(''.join(s))
	else:
		print(-1)


from collections import deque
from collections import Counter
from sys import stdin
lines = deque((line.strip() for line in stdin.readlines()))

def nextline():
	return lines.popleft()

def types(cast):
	return tuple((int(x) for x in nextline().split()))

def ints():
	return types(int)

def strs():
	return nextline().split()

def removeMod(n, mod):
	for i in range(len(n) - 1, -1, -1):
		if int(n[i]) % 3 == mod:
			if i == 0:
				j = 1
				while j < len(n) - 1 and n[j] == '0':
					j += 1
				return n[j:] if j < len(n) else '-1'
			if len(n) > 1:
				return n[:i] + n[i + 1:]
			return '-1'
	return None

def main():
	n = nextline()
	counts = Counter((int(x) % 3 for x in n))
	if counts[1] % 3 == counts[2] % 3:
		print(n)
	elif (counts[1] - 1) % 3 == counts[2] % 3 and counts[1]:
		if counts[1] == 1 and int(n[0]) % 3 == 1 and (len(n) > 1) and (n[1] == '0') and (counts[2] > 1):
			print(removeMod(removeMod(n, 2), 2))
		else:
			print(removeMod(n, 1))
	elif (counts[2] - 1) % 3 == counts[1] % 3 and counts[2]:
		if counts[2] == 1 and int(n[0]) % 3 == 2 and (len(n) > 1) and (n[1] == '0') and (counts[1] > 1):
			print(removeMod(removeMod(n, 1), 1))
		else:
			print(removeMod(n, 2))
	elif counts[1]:
		print(removeMod(removeMod(n, 1), 1))
	else:
		print(removeMod(removeMod(n, 2), 2))
main()


def checkBeauty(digi):
	total = 0
	for i in range(0, len(digi)):
		total += int(digi[i])
	return total % 3

def modThree(digi):
	one = []
	two = []
	for i in range(0, len(digi)):
		if int(digi[i]) % 3 == 1:
			one.insert(0, i)
		elif int(digi[i]) % 3 == 2:
			two.insert(0, i)
	modResult = []
	modResult.append(one)
	modResult.append(two)
	return modResult

def checkHead(digi):
	if len(digi) == 0:
		return -1
	while len(digi) > 1:
		if int(digi[0]) == 0:
			digi.pop(0)
		else:
			break
	return ''.join(digi)

def checkOneOrTwo(digiOne, digiTwo):
	resultOne = checkHead(digiOne)
	resultTwo = checkHead(digiTwo)
	finalResult = -1
	if resultOne == -1 and resultTwo != -1:
		finalResult = resultTwo
	elif resultOne != -1 and resultTwo == -1:
		finalResult = resultOne
	elif resultOne != -1 and resultTwo != -1:
		finalResult = resultTwo if len(resultTwo) > len(resultOne) else resultOne
	return finalResult

def beauty(numDel):
	delOne = []
	delTwo = []
	if numDel == 0:
		print(checkHead(x))
		return
	elif numDel == 1:
		if len(modResult[0]) != 0:
			delOne = x[:]
			delOne.pop(modResult[0][0])
			if len(delOne) != 0 and int(delOne[0]) != 0:
				print(checkHead(delOne))
				return
		if len(modResult[1]) <= 1:
			print(checkHead(delOne))
			return
		elif len(modResult[1]) > 1:
			delTwo = x[:]
			delTwo.pop(modResult[1][0])
			delTwo.pop(modResult[1][1])
			if len(delTwo) != 0 and int(delTwo[0]) != 0:
				print(checkHead(delTwo))
				return
			else:
				print(checkOneOrTwo(delOne, delTwo))
				return
	else:
		if len(modResult[1]) != 0:
			delTwo = x[:]
			delTwo.pop(modResult[1][0])
			if len(delTwo) != 0 and int(delTwo[0]) != 0:
				print(checkHead(delTwo))
				return
		if len(modResult[0]) <= 1:
			print(checkHead(delTwo))
			return
		elif len(modResult[0]) > 1:
			delOne = x[:]
			delOne.pop(modResult[0][0])
			delOne.pop(modResult[0][1])
			if len(delOne) != 0 and int(delOne[0]) != 0:
				print(checkHead(delOne))
				return
			else:
				print(checkOneOrTwo(delOne, delTwo))
				return
x = list(input())
modResult = modThree(x)
numToDel = checkBeauty(x)
beauty(numToDel)


def f(t):
	(i, n) = (0, len(t) - 1)
	while i < n and t[i] == '0':
		i += 1
	return t[i:]
t = input()
n = len(t) - 1
p = [int(q) % 3 for q in t][::-1]
s = sum(p) % 3
if s == 0:
	print(t)
	exit()
u = v = ''
if s in p:
	i = n - p.index(s)
	u = f(t[:i] + t[i + 1:])
s = 3 - s
if p.count(s) > 1:
	i = n - p.index(s)
	j = n - p.index(s, n - i + 1)
	v = f(t[:j] + t[j + 1:i] + t[i + 1:])
t = u if len(u) > len(v) else v
print(t if t else -1)


def erase_digits(n, remainder, count):
	cnt = 0
	i = len(n) - 1
	s = n
	while i >= 0 and cnt < count:
		digit = int(s[i])
		if digit % 3 == remainder:
			cnt += 1
			s = s[:i] + s[i + 1:]
		i -= 1
	if len(s) == len(n) or cnt < count:
		return ''
	while len(s) > 1 and s[0] == '0':
		s = s[1:]
	return s
n = input()
sum = 0
for i in range(len(n)):
	digit = int(n[i])
	sum = (sum + digit) % 3
if sum == 0:
	print(n)
else:
	n1 = erase_digits(n, sum, 1)
	n2 = erase_digits(n, 3 - sum, 2)
	if len(n1) > 0 and len(n1) > len(n2):
		print(n1)
	elif len(n2) > 0:
		print(n2)
	else:
		print(-1)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
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')

def main():
	n = str(input())
	s = 0
	cnt = [0] * 3
	(z, o, t) = ([], [], [])
	for i in range(len(n)):
		s += int(n[i])
		tmp = int(n[i]) % 3
		if tmp == 0:
			z.append(i)
		elif tmp == 1:
			o.append(i)
		else:
			t.append(i)
		cnt[tmp] += 1
	m = s % 3
	three = []
	if m == 0:
		print(n)
	elif m == 1:
		if cnt[1] > 0 and sum(cnt) >= cnt[1]:
			if sum(cnt) <= 1:
				print(-1)
				return
			else:
				ans = [n[i] for i in range(len(n)) if i != o[-1]]
				res = ''.join([str(x) for x in ans]).lstrip('0')
				if len(res) == 0:
					three.append('0')
				else:
					three.append(res)
		if cnt[2] > 1 and sum(cnt) >= cnt[2]:
			if sum(cnt) <= 2:
				print(-1)
				return
			else:
				ans = [n[i] for i in range(len(n)) if i != t[-2] and i != t[-1]]
				res = ''.join([str(x) for x in ans]).lstrip('0')
				if len(res) == 0:
					three.append('0')
				else:
					three.append(res)
		if len(three) == 0:
			print(-1)
		else:
			print(sorted(three, key=lambda x: len(x))[-1])
	else:
		if cnt[2] > 0 and sum(cnt) >= cnt[2]:
			if sum(cnt) <= 1:
				print(-1)
				return
			else:
				ans = [n[i] for i in range(len(n)) if i != t[-1]]
				res = ''.join([str(x) for x in ans]).lstrip('0')
				if len(res) == 0:
					three.append('0')
				else:
					three.append(res)
		if cnt[1] > 1 and sum(cnt) >= cnt[1]:
			if sum(cnt) <= 2:
				print(-1)
				return
			else:
				ans = [n[i] for i in range(len(n)) if i != o[-2] and i != o[-1]]
				res = ''.join([str(x) for x in ans]).lstrip('0')
				if len(res) == 0:
					three.append('0')
				else:
					three.append(res)
		if len(three) == 0:
			print(-1)
		else:
			print(sorted(three, key=lambda x: len(x))[-1])
main()


def countremove(n, mod):
	res = 0
	for i in range(len(n)):
		if mod[len(n) - 1 - i] == 3:
			continue
		if n[i] == '0':
			res += 1
		else:
			break
	return res

def markremove(n):
	mod = [int(x) % 3 for x in n][::-1]
	excess = sum(mod) % 3
	ones = mod.count(1)
	twos = mod.count(2)
	if excess == 1:
		(remove1, remove2) = (104206969, 104206969)
		if ones > 0:
			mod[mod.index(1)] = 3
			remove1 = countremove(n, mod) + 1
			mod[mod.index(3)] = 1
		if twos > 1:
			mod[mod.index(2)] = 3
			mod[mod.index(2)] = 3
			remove2 = countremove(n, mod) + 2
			mod[mod.index(3)] = 2
			mod[mod.index(3)] = 2
		if remove1 < remove2:
			mod[mod.index(1)] = 3
		else:
			mod[mod.index(2)] = 3
			mod[mod.index(2)] = 3
	elif excess == 2:
		(remove1, remove2) = (104206969, 104206969)
		if twos > 0:
			mod[mod.index(2)] = 3
			remove1 = countremove(n, mod) + 1
			mod[mod.index(3)] = 2
		if ones > 1:
			mod[mod.index(1)] = 3
			mod[mod.index(1)] = 3
			remove2 = countremove(n, mod) + 2
			mod[mod.index(3)] = 1
			mod[mod.index(3)] = 1
		if remove1 < remove2:
			mod[mod.index(2)] = 3
		else:
			mod[mod.index(1)] = 3
			mod[mod.index(1)] = 3
	return mod

def buildans(n, mod):
	excess = sum(mod) % 3
	ans = ''
	for i in range(len(n) - 1, -1, -1):
		if mod[i] != 3:
			ans += n[len(n) - 1 - i]
	remove0s = 0
	for i in range(len(ans)):
		remove0s = i
		if ans[i] != '0':
			break
	if remove0s > 0:
		ans = ans[remove0s:]
	if excess != 0 or ans == '':
		ans = '-1'
	return ans
n = input()
mod = markremove(n)
print(buildans(n, mod))


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())


from copy import deepcopy
w = input()
a = [int(c) for c in w]
sm = sum(a)

def process_res():
	res = ''
	non_zero = False
	for n in range(len(a)):
		if not non_zero and n == len(a) - 1 and (a[n] == 0):
			res += '0'
			break
		if not non_zero and a[n] != 0:
			non_zero = True
			res += str(a[n])
		elif non_zero:
			res += str(a[n])
	return res
if sm % 3 == 0:
	print(w)
else:
	if sm % 3 == 1:
		n1 = 1
		n2 = 2
	else:
		n1 = 2
		n2 = 1
	res1 = ''
	for i in range(len(a) - 1, -1, -1):
		if a[i] % 3 == n1:
			b = deepcopy(a)
			a.pop(i)
			res1 = process_res()
			a = deepcopy(b)
			break
	cnt = 0
	frst = None
	res2 = ''
	for i in range(len(a) - 1, -1, -1):
		if a[i] % 3 == n2:
			cnt += 1
			if cnt == 2:
				b = deepcopy(a)
				a.pop(max(i, frst))
				a.pop(min(i, frst))
				res2 = process_res()
				a = deepcopy(b)
				break
			elif cnt == 1:
				frst = i
	if res1 == '' and res2 == '':
		print(-1)
	elif len(res1) > len(res2):
		print(res1)
	else:
		print(res2)


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:])


n = input()
if len(n) < 3:
	if len(n) == 1:
		if int(n) % 3 != 0:
			print(-1)
		else:
			print(n)
	if len(n) == 2:
		if (int(n[0]) + int(n[1])) % 3 == 0:
			print(n)
		elif int(n[0]) % 3 == 0:
			print(n[0])
		elif int(n[1]) % 3 == 0:
			print(n[1])
		else:
			print(-1)
else:
	for i in range(len(n)):
		if n[i] != '0':
			n = n[i:]
			break
	if n[0] == '0':
		n = '0'
	dig_sum = sum([int(x) for x in n]) % 3
	found = dig_sum == 0
	result = '0'
	if not found:
		for i in range(len(n) - 1, -1, -1):
			if (dig_sum - int(n[i])) % 3 == 0:
				found = True
				tmp = n[:i] + n[i + 1:]
				break
		if found:
			for i in range(len(tmp)):
				if tmp[i] != '0':
					tmp = tmp[i:]
					break
			if tmp[0] == '0':
				tmp = '0'
			if found:
				result = tmp
		found_two = False
		if len(n) > 2:
			for i in range(len(n) - 1, -1, -1):
				if int(n[i]) % 3 != 0:
					dig_sum -= int(n[i])
					tmp = n[:i] + n[i + 1:]
					break
			for i in range(len(tmp) - 1, -1, -1):
				if (dig_sum - int(tmp[i])) % 3 == 0:
					found_two = True
					tmp = tmp[:i] + tmp[i + 1:]
					break
			if found_two:
				for i in range(len(tmp)):
					if tmp[i] != '0':
						tmp = tmp[i:]
						break
				if tmp[0] == '0':
					tmp = '0'
				if not found:
					result = tmp
				elif found_two and len(result) < len(tmp):
					result = tmp
	else:
		result = n
	print(result)


import sys
import bisect
from bisect import bisect_left as lb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
sflo = lambda : list(map(float, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
mod1 = 998244353
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
ans = 0

def remove_rem(a, rem, cout):
	temp = list(a)
	i = len(temp) - 1
	cc = 0
	while i >= 0 and cc < cout:
		if int(temp[i]) % 3 == rem:
			cc += 1
			temp = temp[:i] + temp[i + 1:]
		i -= 1
	if cc != cout:
		return ''
	while len(temp) > 1 and temp[0] == '0':
		temp = temp[1:]
	return ''.join(temp)

def hnbhai(tc):
	s = sa()
	b = list(map(int, list(s)))
	rem = sum(b) % 3
	if rem == 0:
		print(s)
		return
	x = remove_rem(s, rem, 1)
	y = remove_rem(s, 3 - rem, 2)
	if len(x) == 0 and len(y) == 0:
		print(-1)
		return
	elif len(x) > len(y):
		print(x)
		return
	print(y)
for _ in range(1):
	hnbhai(_ + 1)


a = [int(x) for x in input()]
n = len(a)
rem = sum(a) % 3
a1 = a[:]
r1 = rem
i = n - 1
while r1 > 0 and i >= 0:
	if a1[i] % 3 == 2:
		if r1 == 1:
			a1[i] = -1
			r1 = 2
		else:
			a1[i] = -1
			r1 = 0
	i -= 1
cb = n + 1
ab = []
if r1 == 0:
	c1 = 0
	i = 0
	while i < n and (a1[i] == -1 or a1[i] == 0):
		c1 += 1
		i += 1
	while i < n:
		c1 += 1 if a1[i] == -1 else 0
		i += 1
	cb = c1
	ab = a1
a2 = a[:]
r2 = rem
i = n - 1
while r2 > 0 and i >= 0:
	if a2[i] % 3 == 1:
		a2[i] = -1
		r2 -= 1
	i -= 1
if r2 == 0:
	c1 = 0
	i = 0
	while i < n and (a2[i] == -1 or a2[i] == 0):
		c1 += 1
		i += 1
	while i < n:
		c1 += 1 if a2[i] == -1 else 0
		i += 1
	if c1 < cb:
		cb = c1
		ab = a2
a3 = a[:]
r3 = rem
i = n - 1
while r3 > 0 and i >= 0:
	d1 = 1
	d2 = 2
	if a3[i] % 3 == 1 and d1 > 0:
		a3[i] = -1
		r3 -= 1
	if a3[i] % 3 == 2 and d2 > 0:
		if r3 == 1:
			a3[i] = -1
			r3 = 2
		else:
			a3[i] = -1
			r3 = 0
	i -= 1
if r3 == 0:
	c1 = 0
	i = 0
	while i < n and (a3[i] == -1 or a3[i] == 0):
		c1 += 1
		i += 1
	while i < n:
		c1 += 1 if a3[i] == -1 else 0
		i += 1
	if c1 < cb:
		cb = c1
		ab = a3
if cb == n + 1:
	print(-1)
else:
	i = 0
	while i < n and (ab[i] == 0 or ab[i] == -1):
		i += 1
	if i == n:
		print(0 if sum(ab) != -n else -1)
	else:
		while i < n:
			if ab[i] != -1:
				print(ab[i], end='')
			i += 1


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)


import sys
from collections import Counter
s = input()
d = [int(x) % 3 for x in s]
c = Counter(d)
sum_digits = sum((int(x) for x in s))
if sum_digits % 3 == 0:
	print(s)
else:
	main = sum_digits % 3
	aux = 3 - main
	rem = len(s)
	skip = [True] * len(s)
	if c[main] == 1 and d[0] == main:
		rem = 1
		skip = [True] + [False] * (len(s) - 1)
		cur = 1
		while cur < len(s) and s[cur] == '0':
			skip[cur] = True
			cur += 1
			rem += 1
	elif c[main] >= 1:
		rem = 1
		skip = [False] * len(s)
		cur = 1
		while d[cur] != main:
			cur += 1
		skip[cur] = True
	if rem > 1 and c[aux] == 2 and (d[0] == aux):
		rem = 1
		skip = [True] + [False] * (len(s) - 1)
		cur = 1
		while s[cur] == '0':
			skip[cur] = True
			cur += 1
			rem += 1
		lz = True
		while d[cur] != aux:
			cur += 1
			lz = False
		skip[cur] = True
		cur += 1
		if lz:
			while cur < len(s) and s[cur] == '0':
				skip[cur] = True
				cur += 1
				rem += 1
	elif rem > 1 and c[aux] >= 2:
		rem = 2
		skip = [False] * len(s)
		(cnt, cur) = (0, 1)
		while cnt < 2:
			if d[cur] == aux:
				skip[cur] = True
				cnt += 1
			cur += 1
	if all(skip):
		print(0 if '0' in s else -1)
		sys.exit(0)
	for (i, ch) in enumerate(s):
		if not skip[i]:
			print(ch, end='')


n = input()
nums = [int(i) % 3 for i in n]
ones = nums.count(1)
twos = nums.count(2)
dis = (ones - twos) % 3

def solve():
	global n
	global nums
	global dis
	if dis == 0:
		return n
	t = rfind(dis)
	if t > 0 or (t == 0 and len(n) > 1 and (nums[t + 1] != 0)):
		return n[:t] + n[t + 1:]
	(g, c) = findtwo(3 - dis)
	if g > 0:
		return n[:g] + n[g + 1:c] + n[c + 1:]
	if nums[0] == dis:
		return tr(n[1:])
	return tr(n[:g] + n[g + 1:c] + n[c + 1:])

def rfind(t):
	c = -1
	for i in range(len(n)):
		if nums[i] == t:
			c = i
	return c

def findtwo(tr):
	c = -1
	g = -1
	for i in range(len(n)):
		if nums[i] == tr:
			c = g
			g = i
	return (c, g)

def tr(s):
	if len(s) == 0:
		return '-1'
	i = 0
	while i < len(s) - 1 and s[i] == '0':
		i += 1
	return s[i:]
print(solve())


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='')


def clean_trailing_zeros(a):
	if not a:
		return a
	i = len(a) - 1
	while i >= 1 and a[i] == 0:
		i -= 1
	return a[:i + 1]

def remove_digits(a, i1, i2):
	if i1:
		i1 = i1[0]
		result1 = clean_trailing_zeros(a[:i1] + a[i1 + 1:])
	else:
		result1 = []
	if len(i2) >= 2:
		(i21, i22) = i2[:2]
		result2 = clean_trailing_zeros(a[:i21] + a[i21 + 1:i22] + a[i22 + 1:])
	else:
		result2 = []
	if len(result1) > len(result2):
		return result1
	else:
		return result2

def main():
	a = map(int, input())
	a = list(a)[::-1]
	if len(a) == 1:
		a = a[0]
		if a % 3 == 0:
			return a
		else:
			return -1
	a_mod_3 = [x % 3 for x in a]
	sum_mod_3 = sum(a) % 3
	if sum_mod_3 == 0:
		return ''.join(map(str, a[::-1]))
	if sum_mod_3 == 1:
		i1 = [i for (i, x) in enumerate(a_mod_3) if x == 1]
		i2 = [i for (i, x) in enumerate(a_mod_3) if x == 2]
	else:
		i1 = [i for (i, x) in enumerate(a_mod_3) if x == 2]
		i2 = [i for (i, x) in enumerate(a_mod_3) if x == 1]
	result = remove_digits(a, i1, i2)
	if result:
		return ''.join(map(str, result[::-1]))
	else:
		return -1
print(main())


s = input()
l = []
n = len(s)
ans = 0
(r1, r2) = ([], [])

def red(l):
	for i in range(len(l)):
		if l[i] != 0:
			return l[i:]
	return l

def pri(l):
	print(''.join([str(k) for k in l]))
	exit()
for i in range(n):
	l.append(int(s[i]))
	ans += l[-1]
	if l[-1] % 3 == 1 and len(r1) < 3:
		r1.append(i)
	if l[-1] % 3 == 2 and len(r2) < 3:
		r2.append(i)
if ans % 3 == 0:
	if sum(l) == 0:
		if len(l) == 0:
			print(-1)
		else:
			print(0)
		exit()
	pri(l)
elif ans % 3 == 1:
	(l1, l2) = ([], [])
	if len(r1) > 0:
		t = r1[-1]
		l1 = l[:t] + l[t + 1:]
	if len(r2) > 1:
		(p, q) = (r2[-1], r2[-2])
		l2 = l[:q] + l[q + 1:p] + l[p + 1:]
	if len(r1) == 0 and len(r2) < 2:
		print(-1)
		exit()
	l1 = red(l1)
	l2 = red(l2)
	if len(l1) > len(l2):
		l = l1
	else:
		l = l2
	if sum(l) == 0:
		if len(l) == 0:
			print(-1)
		else:
			print(0)
		exit()
	pri(l)
else:
	(l1, l2) = ([], [])
	if len(r2) > 0:
		t = r2[-1]
		l1 = l[:t] + l[t + 1:]
	if len(r1) > 1:
		(p, q) = (r1[-1], r1[-2])
		l2 = l[:q] + l[q + 1:p] + l[p + 1:]
	if len(r2) == 0 and len(r1) < 2:
		print(-1)
		exit()
	l1 = red(l1)
	l2 = red(l2)
	if len(l1) > len(l2):
		l = l1
	else:
		l = l2
	if sum(l) == 0:
		if len(l) == 0:
			print(-1)
		else:
			print(0)
		exit()
	pri(l)


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)


sn = input().strip()
n = len(sn)

def rem3(sn):
	return sum(map(int, sn)) % 3

def buildn(sn, com, start=0):
	return ''.join((sn[start + i] for i in com))

def subminbeau2(sn, start=0):
	n = len(sn)
	r3 = rem3(sn[start:])
	sn3 = ''.join((str(int(c) % 3) for c in sn))
	nums3 = set(sn3[1 + start:])
	if r3 == 0:
		return (0, sn[start:])
	elif str(r3) in nums3:
		p1 = sn3.find(str(r3), start + 1)
		return (1, sn[start:p1] + sn[p1 + 1:])
	elif sn3[1 + start:].count(str(3 - r3)) >= 2:
		p1 = sn3.find(str(3 - r3), start + 1)
		p2 = sn3.find(str(3 - r3), p1 + 1)
		return (2, sn[start:p1] + sn[p1 + 1:p2] + sn[p2 + 1:])
	else:
		return (-1, None)

def minbeau(sn):
	n = len(sn)
	nnzs = [i for (i, c) in enumerate(sn) if c != '0']
	res = (n, '')
	for i in nnzs[:3]:
		(td, d3n) = subminbeau2(sn, i)
		if td >= 0:
			res = min(res, (i + td, d3n))
	if res[0] < n:
		return res[1]
	elif '0' in sn:
		return '0'
	else:
		return '-1'
print(minbeau(sn))


def remove_1_remainder(num, num_sum):
	dig_idx = len(num) - 1
	to_remove = []
	found = True
	while num_sum % 3 != 0:
		if dig_idx < 0 or len(num) - len(to_remove) == 1:
			found = False
			break
		curr_num = int(num[dig_idx])
		if curr_num % 3 == 1:
			to_remove.append(dig_idx)
			num_sum -= curr_num
		dig_idx -= 1
	return (to_remove, found)

def remove_2_remainder(num, num_sum):
	dig_idx = len(num) - 1
	to_remove = []
	found = True
	while num_sum % 3 != 0:
		if dig_idx < 0 or len(num) - len(to_remove) == 1:
			found = False
			break
		curr_num = int(num[dig_idx])
		if curr_num % 3 == 2:
			to_remove.append(dig_idx)
			num_sum -= curr_num
		dig_idx -= 1
	return (to_remove, found)

def rebuild_string(num, indices):
	new_str = ''
	indices = set(indices)
	for idx in range(len(num)):
		if idx in indices:
			continue
		new_str += num[idx]
	while len(new_str) != 0 and new_str[0] == '0':
		if new_str == '0':
			return new_str
		new_str = new_str[1:]
	return new_str
num = input()
num_sum = sum((int(p) for p in num))
ops = 0
dig_idx = len(num) - 1
remainder = num_sum % 3
to_print = None
if remainder == 0:
	to_print = num
elif len(num) == 1:
	to_print = -1
elif remainder == 1:
	(idx_removed, is_valid) = remove_1_remainder(num, num_sum)
	(idx_removed_2, is_valid_2) = remove_2_remainder(num, num_sum)
	if not is_valid and (not is_valid_2):
		to_print = -1
	elif is_valid and is_valid_2:
		(str_1, str_2) = (rebuild_string(num, idx_removed), rebuild_string(num, idx_removed_2))
		to_print = str_1 if len(str_1) > len(str_2) else str_2
	elif is_valid:
		to_print = rebuild_string(num, idx_removed)
	else:
		to_print = rebuild_string(num, idx_removed_2)
elif remainder == 2:
	(idx_removed, is_valid) = remove_1_remainder(num, num_sum)
	(idx_removed_2, is_valid_2) = remove_2_remainder(num, num_sum)
	if not is_valid and (not is_valid_2):
		to_print = -1
	elif is_valid and is_valid_2:
		(str_1, str_2) = (rebuild_string(num, idx_removed), rebuild_string(num, idx_removed_2))
		to_print = str_1 if len(str_1) > len(str_2) else str_2
	elif is_valid:
		to_print = rebuild_string(num, idx_removed)
	else:
		to_print = rebuild_string(num, idx_removed_2)
if to_print is None or (isinstance(to_print, str) and len(to_print) > 1 and (to_print[0] == '0')):
	raise Exception()
print(to_print)


s = input()
L = len(s)
S = 0
zero = []
one = []
two = []
for i in range(L):
	x = int(s[i])
	S = (S + x) % 3
	if x % 3 == 1:
		one.append(i)
	elif x % 3 == 2:
		two.append(i)
	elif x == 0:
		zero.append(i)
if S == 0:
	print(s)
elif S == 1:
	if len(one) >= 1 and (one[-1] > 0 or len(zero) == 0 or zero[0] > 1) and (L > 1):
		print(s[:one[-1]] + s[one[-1] + 1:])
	elif len(two) >= 2 and two[-2] > 0 and (L > 2):
		print(s[:two[-2]] + s[two[-2] + 1:two[-1]] + s[two[-1] + 1:])
	elif len(one) == 1 and L > 1:
		print(int(s[1:]))
	elif len(two) == 2 and L > 2:
		print(int(s[1:two[-1]] + s[two[-1] + 1:]))
	else:
		print(-1)
elif len(two) >= 1 and (two[-1] > 0 or len(zero) == 0 or zero[0] > 1) and (L > 1):
	print(s[:two[-1]] + s[two[-1] + 1:])
elif len(one) >= 2 and one[-2] > 0 and (L > 2):
	print(s[:one[-2]] + s[one[-2] + 1:one[-1]] + s[one[-1] + 1:])
elif len(two) == 1 and L > 1:
	print(int(s[1:]))
elif len(one) == 2 and L > 2:
	print(int(s[1:one[-1]] + s[one[-1] + 1:]))
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)


num = list(map(int, list(input())))
num2 = sum(num) % 3
j = 0
for i in range(1, len(num)):
	if num[i] % 3 == num2:
		j = i
num3 = 3 - num2
z = []
for i in range(1, len(num)):
	if num[i] % 3 == num3:
		z += [i]
num4 = num[0] % 3
if num2 == 0:
	for i in range(len(num)):
		print(num[i], end='')
elif j != 0:
	for i in range(len(num)):
		if i != j:
			print(num[i], end='')
elif num4 == num2 and len(num) != 1 and (len(z) < 2 or num[1] != 0):
	i2 = len(num)
	for i in range(1, len(num)):
		if num[i] != 0:
			i2 = i
			break
	for i in range(i2, len(num)):
		print(num[i], end='')
	if i2 == len(num):
		print(0)
elif len(z) >= 2:
	for i in range(len(num)):
		if i != z[0] and i != z[1]:
			print(num[i], end='')
elif num4 == 0:
	print(-1)
elif num4 == num3 and len(z) == 0:
	print(-1)
elif len(num) < 3:
	print(-1)
else:
	i2 = len(num)
	for i in range(1, len(num)):
		if num[i] != 0:
			i2 = i
			break
	if i2 == z[0] and z[0] != len(num) - 1:
		for i in range(i2 + 1, len(num)):
			i2 = i
			if num[i] != 0:
				i2 = i
				break
		if i2 == len(num):
			print(0)
		else:
			for i in range(i2, len(num)):
				print(num[i], end='')
	elif i2 == z[0] and z[0] == len(num) - 1:
		print(0)
	else:
		for i in range(i2, len(num)):
			if i != z[0]:
				print(num[i], end='')


s = input()

def rm(s, m):
	if s == None or len(s) == 1:
		return None
	i = len(s) - 1
	while i >= 0:
		if int(s[i]) % 3 == m:
			break
		i -= 1
	if i == -1:
		return None
	elif i == 0:
		k = i + 1
		while k < len(s) and s[k] == '0':
			k += 1
		if k == len(s):
			return '0'
		else:
			return s[k:]
	elif i == len(s) - 1:
		return s[:i]
	else:
		return s[:i] + s[i + 1:]

def ans(s):
	s_sum = 0
	i = 0
	while i < len(s):
		s_sum += int(s[i])
		s_sum = s_sum % 3
		i += 1
	if s_sum == 0:
		return s
	elif s_sum == 1:
		s1 = rm(s, 1)
		s2 = rm(rm(s, 2), 2)
		if s1 == None and s2 == None:
			return -1
		elif s1 == None:
			return s2
		elif s2 == None:
			return s1
		elif len(s1) > len(s2):
			return s1
		else:
			return s2
	elif s_sum == 2:
		s1 = rm(s, 2)
		s2 = rm(rm(s, 1), 1)
		if s1 == None and s2 == None:
			return -1
		elif s1 == None:
			return s2
		elif s2 == None:
			return s1
		elif len(s1) > len(s2):
			return s1
		else:
			return s2
t = ans(s)
if t == None:
	print(-1)
else:
	print(t)