n = input() m = list(set(list(n))) if n[:2] != '13' or '1' in n[2:] or len(m) != 2: print(-1) else: print(n.count('3'))