Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.

A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s.

Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end.

Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. 

You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend.

Input

You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.

Output

Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes.

Examples

Input

fixprefixsuffix


Output

fix

Input

abcdabc


Output

Just a legend

Difficulty: MEDIUM_HARD
Tags: String algorithms

Sorting

Dynamic programming
Skill types: Dynamic programming

Sorting

Starter code:

Solutions:
from fractions import Fraction
import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate

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

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def zfunction(s):
	n = len(s)
	(l, r) = (0, 0)
	Z = [0] * n
	for i in range(1, n):
		if i <= r:
			Z[i] = min(r - i + 1, Z[i - l])
		while i + Z[i] < n and s[Z[i]] == s[i + Z[i]]:
			Z[i] += 1
		if i + Z[i] - 1 > r:
			(l, r) = (i, i + Z[i] - 1)
	return Z
s = input()
n = len(s)
Z = zfunction(s)
third = []
for i in range(n):
	if i + Z[i] == n:
		third.append(Z[i])
ll = len(third)
ans = ''
if ll == 0:
	ans = 'Just a legend'
elif ll == 1:
	if Z.count(third[0]) >= 2 or max(Z) > third[0]:
		ans = s[:third[0]]
	else:
		ans = 'Just a legend'
elif Z.count(third[0]) >= 2 or max(Z) > third[0]:
	ans = s[:third[0]]
else:
	ans = s[:third[1]]
print(ans)


s = input()
(i, n) = (0, len(s))
p = [-1] + [0] * n
for j in range(1, n):
	while i + 1 and s[i] != s[j]:
		i = p[i]
	i += 1
	p[j + 1] = i
k = p.pop()
print(s[:k] if k and k in p else s[:p[k]] if p[k] > 0 else 'Just a legend')


import sys
from array import array
from collections import Counter

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def z_algorithm(S):
	n = len(S)
	Z = [n] + [0] * (n - 1)
	(i, j) = (1, 0)
	while i < n:
		while i + j < n and S[j] == S[i + j]:
			j += 1
		Z[i] = j
		if j == 0:
			i += 1
			continue
		k = 1
		while k < j and k + Z[k] < j:
			Z[i + k] = Z[k]
			k += 1
		i += k
		j -= k
	return Z
s = input().rstrip()
n = len(s)
z = z_algorithm(s)
freq = Counter(z[1:])
ans = -1
max_pre = max(z[1:], default=0)
for slen in range(1, n - 1):
	freq[z[n - slen]] -= 1
	while freq[max_pre] == 0:
		max_pre -= 1
	if max_pre >= slen and z[n - slen] == slen:
		ans = slen
print(s[:ans] if ans > -1 else 'Just a legend')


s = input()
n = len(s)
p = [-1] * n
mark = [False] * n
q = -1
for i in range(1, n):
	while q >= 0 and s[i] != s[q + 1]:
		q = p[q]
	if s[i] == s[q + 1]:
		q += 1
		p[i] = q
	if q >= 0 and i < n - 1:
		mark[q] = True
q = p[n - 1]
while q >= 0:
	if s[0:q + 1] == s[n - q - 1:] and mark[q]:
		print(s[0:q + 1])
		exit()
	q = p[q]
print('Just a legend')


(s, k, l) = (input(), -1, [-1])
for c in s:
	while k != -1 and s[k] != c:
		k = l[k]
	k += 1
	l += [k]
q = l[-1]
if l.count(q) < 2:
	q = l[q]
if q > 0:
	print(s[:q])
else:
	print('Just a legend')


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

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

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest
sys.setrecursionlimit(111111)
INF = 999999999999999999999999
alphabets = 'abcdefghijklmnopqrstuvwxyz'

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))

class SegTree:

	def __init__(self, n):
		self.N = 1 << n.bit_length()
		self.tree = [0] * (self.N << 1)

	def update(self, i, j, v):
		i += self.N
		j += self.N
		while i <= j:
			if i % 2 == 1:
				self.tree[i] += v
			if j % 2 == 0:
				self.tree[j] += v
			(i, j) = (i + 1 >> 1, j - 1 >> 1)

	def query(self, i):
		v = 0
		i += self.N
		while i > 0:
			v += self.tree[i]
			i >>= 1
		return v

def SieveOfEratosthenes(limit):
	isPrime = [True] * (limit + 1)
	isPrime[0] = isPrime[1] = False
	primes = []
	for i in range(2, limit + 1):
		if not isPrime[i]:
			continue
		primes += [i]
		for j in range(i * i, limit + 1, i):
			isPrime[j] = False
	return primes

def main():
	mod = 1000000007
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		s = list(rs())

		def z_function(S):
			n = len(S)
			Z = [0] * n
			l = r = 0
			for i in range(1, n):
				z = Z[i - l]
				if i + z >= r:
					z = max(r - i, 0)
					while i + z < n and S[z] == S[i + z]:
						z += 1
					(l, r) = (i, i + z)
				Z[i] = z
			Z[0] = n
			return Z
		if len(s) < 3:
			ws('Just a legend')
		else:
			z_values = z_function(s)
			mxpref = [0] * len(s)
			mx = 0
			for i in range(1, len(s)):
				mx = max(mx, z_values[i])
				mxpref[i] = mx
			ansind = 0
			for i in range(1, len(s) - 1):
				if i + 1 + z_values[i + 1] == len(s) and mxpref[i] >= z_values[i + 1]:
					ansind = i + 1
					break
			if ansind:
				ws(''.join(s[ansind:]))
			else:
				ws('Just a legend')
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			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, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			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()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

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

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


s = input()
n = len(s)
p = [0] * (n + 1)
i = 0
j = 1
while j < n:
	if s[j] == s[i]:
		j += 1
		i += 1
		p[j] = i
	elif i:
		i = p[i]
	else:
		j += 1
a = p.pop()
b = p[a]
if a and a in p:
	print(s[:a])
elif b:
	print(s[:b])
else:
	print('Just a legend')


import os
import sys
from io import BytesIO, IOBase

def pie(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j > 0 and s[i] != s[j]:
			j = pi[j - 1]
		j += s[i] == s[j]
		pi[i] = j
	return pi

def main():
	s = input().rstrip()
	pi = pie(s)
	if pi[-1]:
		n = len(s)
		for i in range(n):
			if i != n - 1 and pi[i] == pi[-1]:
				print(s[:pi[-1]])
				return
		if pi[pi[-1] - 1]:
			print(s[:pi[pi[-1] - 1]])
			return
	print('Just a legend')
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()


def prefix(lsa, n):
	index = 0
	i = 1
	while i < n:
		if s[i] == s[index]:
			lsa[i] = index + 1
			i += 1
			index += 1
		elif index == 0:
			lsa[i] = 0
			i += 1
		else:
			index = lsa[index - 1]
s = input()
n = len(s)
lsa = [0] * n
prefix(lsa, n)
if lsa[-1] == 0:
	print('Just a legend')
else:
	for i in range(n - 1):
		if lsa[i] == lsa[-1]:
			print(s[:lsa[i]])
			exit()
	if lsa[lsa[-1] - 1] == 0:
		print('Just a legend')
	else:
		print(s[:lsa[lsa[-1] - 1]])


from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

def ceil(a, b):
	return (a + b - 1) // b

def z_algo(s):
	n = len(s)
	z = [0] * n
	l = r = 0
	for i in range(n):
		if i > r:
			l = r = i
			while r < n and s[r - l] == s[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			j = i - l
			if z[j] + i > r:
				l = i
				while r < n and s[r - l] == s[r]:
					r += 1
				z[i] = r - l
				r -= 1
			else:
				z[i] = z[j]
	return z

def solve():
	s = si()
	x = z_algo(s)
	n = len(x)
	f = dd(int)
	st = set()
	for i in x:
		if i > 0:
			f[i] += 1
			st.add(i)
	st = list(st)
	st.sort(reverse=True)
	cnt = 0
	for i in st:
		cnt += f[i]
		if cnt >= 2 and x[-i] == i:
			print(s[-i:])
			return
	print('Just a legend')
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


import sys
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')

def ComputeLPSArray(pat):
	M = len(pat)
	lps = [0] * M
	length = 0
	i = 1
	while i < M:
		if pat[i] == pat[length]:
			lps[i] = length + 1
			length += 1
			i += 1
		elif length != 0:
			length = lps[length - 1]
		else:
			lps[i] = 0
			i += 1
	return lps
string = input().strip()
lps = ComputeLPSArray(string)
(x, y) = (-1, -1)
x = lps[lps[-1] - 1]
if lps[-1] > 0 and lps.count(lps[-1]) > 1:
	y = lps[-1]
k = max(x, y)
if k > 0:
	print(string[:k])
else:
	print('Just a legend')


from collections import defaultdict, Counter, deque
from math import sqrt, log10, log, floor, factorial
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations
import sys, io, os
input = sys.stdin.readline
inf = float('inf')
mod = 10 ** 9 + 7

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

def yn(a):
	print('YES' if a else 'NO')
ceil = lambda a, b: (a + b - 1) // b

def z_function(S):
	n = len(S)
	Z = [0] * n
	l = r = 0
	for i in range(1, n):
		z = Z[i - l]
		if i + z >= r:
			z = max(r - i, 0)
			while i + z < n and S[z] == S[i + z]:
				z += 1
			(l, r) = (i, i + z)
		Z[i] = z
	Z[0] = n
	return Z
t = 1
for i in range(t):
	s = input().strip()
	z = z_function(s)
	l = sorted(z[1:])
	ans = -1
	for i in range(len(z) - 1, 0, -1):
		if z[i] == len(z) - i:
			if bisect_left(l, z[i]) < len(l) - 1:
				ans = z[i]
	if ans == -1:
		print('Just a legend')
	else:
		print(s[:ans])


def z_func(s):
	n = len(s)
	z = [0] * n
	(l, r) = (0, 0)
	for i in range(1, n):
		if i <= r:
			z[i] = min(r - i + 1, z[i - l])
		while i + z[i] < n and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z

def solve(s):
	z = z_func(s)
	n = len(z)
	max_substr = 0
	max_suffix = 0
	for i in range(1, n):
		max_substr = max(max_substr, min(z[i], n - i - 1))
	for i in range(n - max_substr, n):
		if i + z[i] == n:
			max_suffix = max(max_suffix, z[i])
	return max_suffix
s = input()
ans = solve(s)
if ans > 0:
	print(s[:ans])
else:
	print('Just a legend')


def longestPrefixSuffix(s):
	global d
	n = len(s)
	lps = [0] * n
	l = 0
	m = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l = l + 1
			lps[i] = l
			i = i + 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i = i + 1
	s = s[::-1]
	lps1 = [0] * n
	l = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l = l + 1
			lps1[i] = l
			i = i + 1
		elif l != 0:
			l = lps1[l - 1]
		else:
			lps1[i] = 0
			i = i + 1
	ans = 0
	for i in range(n - 1):
		if lps[i] == lps1[n - (i - lps[i] + 1) - 1]:
			ans = max(ans, lps[i])
	return ans
q = input()
l = longestPrefixSuffix(q)
if l == 0:
	print('Just a legend')
else:
	print(q[:l])


def z_func(s):
	l = 0
	r = 0
	n = len(s)
	z = [0] * n
	z[0] = n
	for i in range(1, n):
		if i <= r:
			z[i] = min(r - i + 1, z[i - l])
		while i + z[i] < n and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z
string = input()
n = len(string)
z = z_func(string)
l = 0
for i in range(1, n):
	if z[i] == n - i:
		for j in range(1, i):
			if z[j] >= z[i]:
				l = z[i]
				break
	if l > 0:
		break
if l > 0:
	print(string[0:l])
else:
	print('Just a legend')


def get_prefix(string):
	lst = [0] * len(string)
	index = 0
	for i in range(1, len(string)):
		while index > 0 and string[index] != string[i]:
			index = lst[index - 1]
		if string[index] == string[i]:
			index += 1
			lst[i] = index
		else:
			lst[i] = index
	return lst
in_c = input()
lst = get_prefix(in_c)
length = lst[len(in_c) - 1]
found = None
if length != 0:
	for j in range(2):
		for i in range(1, len(in_c) - 1):
			if length == lst[i]:
				found = True
				break
		if not found:
			length = lst[length - 1]
			if length == 0:
				break
		else:
			break
if not found or length == 0:
	print('Just a legend')
else:
	print(in_c[0:length])


from sys import stdin

def findpass(s):
	arr = suff_array(s)
	n = len(s)
	maxidx = arr[n - 1]
	valid = False
	for i in range(n - 1):
		if arr[i] == maxidx:
			valid = True
			break
	if not valid:
		maxidx = arr[maxidx - 1]
	if maxidx == 0:
		return 'Just a legend'
	return s[:maxidx]

def suff_array(s):
	n = len(s)
	table = [0] * n
	(pos, i) = (0, 1)
	while i < n:
		if s[pos] == s[i]:
			table[i] = pos + 1
			pos += 1
			i += 1
		elif pos == 0:
			i += 1
		else:
			pos = table[pos - 1]
	return table
s = stdin.readline().strip()
print(findpass(s))


a = input()
n = len(a)
z = [0] * n
l = 0
r = 0
for i in range(1, n):
	if i <= r:
		z[i] = min(r - i, z[i - l])
	while i + z[i] < n and a[z[i]] == a[i + z[i]]:
		z[i] += 1
	if i + z[i] - 1 >= r:
		r = i + z[i] - 1
		l = i
mx = 0
ans = 0
for i in range(n):
	if z[i] == n - i and mx >= z[i]:
		ans = z[i]
		break
	mx = max(z[i], mx)
if ans == 0:
	print('Just a legend')
else:
	print(a[:ans])


def prefix_function(s):
	pi = [0] * len(s)
	for i in range(1, len(s)):
		j = pi[i - 1]
		while j > 0 and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi
s = input()
pi = prefix_function(s)
n = len(s)
ok = True
if pi[n - 1] != 0 and pi.count(pi[n - 1]) > 1:
	print(s[n - pi[n - 1]:n])
elif pi[pi[n - 1] - 1] > 0:
	print(s[pi[n - 1] - pi[pi[n - 1] - 1]:pi[n - 1]])
else:
	print('Just a legend')


def computeLPSArray(pat, M, lps):
	len = 0
	lps[0]
	i = 1
	while i < M:
		if pat[i] == pat[len]:
			len += 1
			lps[i] = len
			i += 1
		elif len != 0:
			len = lps[len - 1]
		else:
			lps[i] = 0
			i += 1
	return lps
s = input()
m = len(s)
lps = [0] * m
x = computeLPSArray(s, m, lps)
x = [0] + x
a = x[-1]
b = x[a]
ans = ''
for i in range(len(x)):
	if x[i] == a and i != m:
		ans = s[0:a]
if ans == '':
	if b != 0:
		ans = s[0:b]
	else:
		ans = 'Just a legend'
print(ans)


s = input()
longest_matching = [-1]
for c in s[1:]:
	last_matching = longest_matching[-1]
	no_match = False
	while s[last_matching + 1] != c:
		if last_matching != -1:
			last_matching = longest_matching[last_matching]
		else:
			last_matching = last_matching - 1
			break
	longest_matching.append(last_matching + 1)
if longest_matching.count(longest_matching[-1]) < 2:
	ans = longest_matching[longest_matching[-1]] + 1
else:
	ans = longest_matching[-1] + 1
if ans > 0:
	print(s[:ans])
else:
	print('Just a legend')


def KMPSearch(pat, txt):
	M = len(pat)
	N = len(txt)
	lps = [0] * M
	j = 0
	computeLPSArray(pat, M, lps)
	i = 0
	while i < N:
		if pat[j] == txt[i]:
			i += 1
			j += 1
		if j == M:
			print('Found pattern at index ' + str(i - j))
			j = lps[j - 1]
		elif i < N and pat[j] != txt[i]:
			if j != 0:
				j = lps[j - 1]
			else:
				i += 1

def computeLPSArray(pat, M, lps):
	len = 0
	lps[0]
	i = 1
	while i < M:
		if pat[i] == pat[len]:
			len += 1
			lps[i] = len
			i += 1
		elif len != 0:
			len = lps[len - 1]
		else:
			lps[i] = 0
			i += 1
	return lps
s = input()
m = len(s)
lps = [0] * m
x = computeLPSArray(s, m, lps)
x = [0] + x
a = x[-1]
b = x[a]
ans = ''
for i in range(len(x)):
	if x[i] == a and i != m:
		ans = s[0:a]
if ans == '':
	if b != 0:
		ans = s[0:b]
	else:
		ans = 'Just a legend'
print(ans)


def kmpTable(s, n):
	res = [0] * n
	check_idx = 0
	for i in range(1, n):
		while check_idx > 0 and s[check_idx] != s[i]:
			check_idx = res[check_idx - 1]
		if s[check_idx] == s[i]:
			check_idx += 1
		res[i] = check_idx
	return res
s = input()
n = len(s)
kmp_table = kmpTable(s, n)
psize = kmp_table[n - 1]
if psize > 0 and psize in kmp_table[:-1]:
	print(s[:psize])
else:
	psize = kmp_table[psize - 1]
	if psize > 0 and psize in kmp_table[:-1]:
		print(s[:psize])
	else:
		print('Just a legend')


import sys
import random
import bisect
from collections import deque
from queue import PriorityQueue
from math import gcd
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()
mod = 10 ** 9 + 7

def zfunc(s):
	n = len(s)
	z = [0 for i in range(n)]
	l = 0
	r = 0
	for i in range(1, n):
		if i <= r:
			z[i] = min(r - i + 1, z[i - l])
		while i + z[i] < n and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z
s = ip()
n = len(s)
mn = zfunc(s)
fl = 1
x1 = -1
for i in range(1, n):
	if i + mn[i] == n:
		if x1 >= mn[i]:
			print(s[i:])
			fl = 0
			break
	x1 = max(x1, mn[i])
if fl:
	print('Just a legend')


def KMP(st):
	i = 0
	j = 1
	arr = [0 for i in range(len(st))]
	while j < len(st):
		if s[j] == s[i]:
			arr[j] = i + 1
			i += 1
		else:
			while i > 0:
				i = arr[i - 1]
				if s[i] == s[j]:
					arr[j] = i + 1
					i += 1
					break
		j += 1
	return arr
s = input()
arr = KMP(s)
maxi = arr[-1]
found = arr[maxi - 1]
for i in arr[1:-1]:
	if i == maxi:
		found = maxi
if found == 0:
	print('Just a legend')
else:
	print(s[:found])


from sys import stdin
s = stdin.readline().rstrip()
k = -1
l = [-1]
for c in s:
	while k != -1 and s[k] != c:
		k = l[k]
	k += 1
	l += [k]
q = l[-1]
if l.count(q) < 2:
	q = l[q]
print(s[:q] if q > 0 else 'Just a legend')


def password(str):
	lst = [0] * len(str)
	n = 0
	for i in range(1, len(str)):
		while n > 0 and str[n] != str[i]:
			n = lst[n - 1]
		if str[n] == str[i]:
			n += 1
			lst[i] = n
		else:
			lst[i] = n
	return lst
x = None
in_c = input()
lst = password(in_c)
var = lst[len(in_c) - 1]
if var != 0:
	for j in range(2):
		for i in range(1, len(in_c) - 1):
			if var == lst[i]:
				x = True
				break
		if not x:
			var = lst[var - 1]
			if var == 0:
				break
		else:
			break
if not x or var == 0:
	print('Just a legend')
else:
	print(in_c[0:var])


import sys, math
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input().strip()

def listStr():
	return list(input().strip())
import collections as col
import math

def solve():
	S = getStr()
	N = len(S)
	T = [0] * N
	pos = 1
	cnd = 0
	while pos < N:
		if S[pos] == S[cnd]:
			cnd += 1
			T[pos] = cnd
			pos += 1
		elif cnd:
			cnd = T[cnd - 1]
		else:
			T[pos] = 0
			pos += 1
	temp = T[-1]
	if T[-1] != 0 and T.count(temp) >= 2:
		return ''.join(S[:temp])
	elif T[-1] != 0 and T[T[-1] - 1] > 0:
		return ''.join(S[:T[T[-1] - 1]])
	return 'Just a legend'
print(solve())


def pref(s):
	l = [0] * len(s)
	pos = 0
	for i in range(1, len(s)):
		while pos > 0 and s[pos] != s[i]:
			pos = l[pos - 1]
		if s[pos] == s[i]:
			pos += 1
			l[i] = pos
		else:
			l[i] = pos
	return l
s = input()
l = pref(s)
length = l[len(s) - 1]
ok = None
if length != 0:
	for i in range(2):
		for j in range(1, len(s) - 1):
			if length == l[j]:
				ok = True
				break
		if not ok:
			length = l[length - 1]
			if length == 0:
				break
		else:
			break
if not ok or length == 0:
	print('Just a legend')
else:
	print(s[0:length])


s = input()
n = len(s)
if n == 1 or n == 2:
	print('Just a legend')
else:
	lista = []
	for i in range(n):
		lista.append(0)
	l = 0
	lista[0] = 0
	i = 1
	while i < n:
		if s[i] == s[l]:
			l += 1
			lista[i] = l
			i += 1
		elif l != 0:
			l = lista[l - 1]
		else:
			lista[i] = 0
			i += 1
	ultimo = lista.pop()
	if ultimo == 0:
		print('Just a legend')
	elif ultimo in lista:
		print(s[:ultimo])
	elif lista[ultimo - 1] == 0:
		print('Just a legend')
	elif lista[ultimo - 1]:
		print(s[:lista[ultimo - 1]])
	else:
		print('Just a legend')


s = input()
M = len(s)
lps = [0] * M
len = 0
i = 1
while i < M:
	if s[i] == s[len]:
		len += 1
		lps[i] = len
		i += 1
	elif len != 0:
		len = lps[len - 1]
	else:
		lps[i] = 0
		i += 1
t = lps[-1]
flag = 1
while t:
	for i in range(1, M - 1):
		if lps[i] == t:
			flag = 0
			break
		else:
			flag = 1
	if flag == 0:
		break
	t = lps[t - 1]
print(s[:t]) if flag == 0 else print('Just a legend')


from typing import Tuple

def comp_z(s: str) -> Tuple[int]:
	n = len(s)
	z = [0] * n
	z[0] = n
	(L, R) = (0, 0)
	for i in range(1, n):
		if i >= R:
			L = i
			R = i
			while R < n and s[R] == s[R - L]:
				R += 1
			z[i] = R - L
		else:
			x = R - i
			if x > z[i - L]:
				z[i] = z[i - L]
			else:
				L = i
				while R < n and s[R] == s[R - L]:
					R += 1
				z[i] = R - L
	return tuple(z)

def run():
	s = input()
	n = len(s)
	z = comp_z(s)
	maxz = 0
	res = 0
	for i in range(1, n):
		if z[i] == n - i and maxz >= n - i:
			res = n - i
			break
		maxz = max(maxz, z[i])
	if res == 0:
		print('Just a legend')
	else:
		print(s[:res])
run()


from math import ceil, log2
from collections import Counter
from sys import stdin
input = stdin.readline

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

def run(s):
	i = 1
	j = 0
	arr = [0]
	n = len(s)
	while i < n:
		if s[j] == s[i]:
			j += 1
			arr.append(j)
			i += 1
		elif j != 0:
			j = arr[j - 1]
		else:
			arr.append(0)
			i += 1
	return arr
s = input().strip()
arr = run(s)
l = arr[-1]
if l != 0 and arr.count(l) >= 2:
	print(s[:l])
elif l != 0 and arr[l - 1] != 0:
	print(s[:arr[l - 1]])
else:
	print('Just a legend')


def lcp(text):
	length = len(text)
	lis = [0] * length
	j = 1
	k = 0
	while j < length:
		if text[j] == text[k]:
			lis[j] = k + 1
			j += 1
			k += 1
		elif k > 0:
			k = lis[k - 1]
		else:
			j += 1
	return lis
text = input()
length = len(text)
lis = lcp(text)
idf = 0
for i in range(length - 1):
	if lis[i] == lis[-1] and lis[i] != 0:
		print(text[0:lis[i]])
		idf = 1
		break
if idf == 0:
	if lis[-1] == 0:
		print('Just a legend')
	else:
		maximum = lis[lis[-1] - 1]
		if maximum == 0:
			print('Just a legend')
		else:
			print(text[0:maximum])


(s, k, d) = (input(), -1, [-1])
for ch in s:
	while k != -1 and s[k] != ch:
		k = d[k]
	k += 1
	d.append(k)
c = d[-1]
if d.count(c) < 2:
	c = d[c]
print(('Just a legend', s[:c])[c > 0])


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from collections import defaultdict as dd, deque

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

def mdata():
	return map(int, data().split())
mod = int(1000000000.0 + 7)

def KMPSearch(pat, txt):
	M = len(pat)
	N = len(txt)
	lps = [0] * M
	j = 0
	computeLPSArray(pat, M, lps)
	i = 0
	while i < N:
		if pat[j] == txt[i]:
			i += 1
			j += 1
		if j == M:
			return (pat[:M], lps)
		elif pat[j] != txt[i]:
			if j != 0:
				j = lps[j - 1]
				M -= 1
				lps.pop()
			else:
				i += 1

def computeLPSArray(pat, M, lps):
	l = 0
	lps[0] = 0
	i = 1
	while i < M:
		if pat[i] == pat[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
	return lps
s = data()
n = len(s)
lps1 = [0] * n
lps1 = computeLPSArray(s, n, lps1)
lps2 = [0] * n
lps2 = computeLPSArray(s[::-1], n, lps2)
m = 0
for i in range(n):
	if lps1[i] == lps2[n - 2 - i + lps1[i]]:
		m = max(m, lps1[i])
if m == 0:
	print('Just a legend')
else:
	print(s[:m])


n = input()
hm = [0] * (len(n) + 1)
p = 31
m = len(n)
mod = 10 ** 9 + 9
pow = [0] * (m + 1)
pow[0] = 1
for i in range(1, m):
	pow[i] = pow[i - 1] * p % mod
hm[0] = 1 * (ord(n[0]) - ord('a') + 1)
m = len(n)
s = 0
e = 0
ans = []
a = 0
for i in range(1, m):
	x = ord(n[i]) - ord('a') + 1
	hm[i] = (hm[i - 1] % mod + pow[i] * x % mod) % mod
for j in range(0, m):
	a = hm[j]
	s = m - 1 - j
	e = m - 1
	a = hm[j] * pow[s] % mod
	b = (hm[e] - hm[s - 1] + mod) % mod
	if a == b:
		ans.append(j)

def check(idx):
	id = ans[idx]
	l = 1
	r = id + 1
	while l < m and r < m:
		a = hm[id] * pow[l] % mod
		b = (hm[r] - hm[l - 1] + mod) % mod
		if a == b and r != m - 1:
			return True
		l += 1
		r += 1
	return False
lo = 0
hi = len(ans)
while lo <= hi:
	mi = (lo + hi) // 2
	if check(mi):
		lo = mi + 1
	else:
		hi = mi - 1
if lo - 1 == -1:
	print('Just a legend')
else:
	print(n[:ans[lo - 1] + 1])


from collections import defaultdict

def zeta(s):
	n = len(s)
	z = [0] * n
	(l, r) = (0, 0)
	for i in range(1, n):
		if i <= r:
			z[i] = min(r - i + 1, z[i - l])
		while i + z[i] < n and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if i + z[i] - 1 > r:
			l = i
			r = i + z[i] - 1
	return z
noSol = 'Just a legend'
s = input()
n = len(s)
z = zeta(s)
occ = defaultdict(int)
for i in range(1, len(z)):
	occ[z[i]] += 1
occ = [(i, occ[i]) for i in occ]
occ.sort()
sol = -1
for i in range(1, len(z)):
	if i + z[i] == n:
		x = z[i]
		if occ[-1][0] > x or occ[-1][1] > 1:
			sol = i
			break
print(s[sol:]) if sol != -1 else print(noSol)


import sys
import math as mt
t = 1

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1
for __ in range(t):
	s = input()
	z = [0] * (len(s) + 1)
	d = {}
	n = len(s)
	getZarr(s, z)
	d = []
	maxm = -1
	for i in range(len(s)):
		if i + z[i] != n:
			maxm = max(maxm, z[i])
		else:
			d.append(z[i])
	maxi = -1
	d.sort()
	for i in range(len(s)):
		if i + z[i] == n:
			if maxm >= z[i]:
				maxi = max(maxi, z[i])
			if d[-1] != z[i]:
				maxi = max(maxi, z[i])
	if maxi > 0:
		print(s[:maxi])
	else:
		print('Just a legend')


def knuthMorrisonPreprocess(pattern, m):
	fail = [0] * m
	i = 0
	j = 1
	fail[0] = 0
	while j < m:
		if pattern[i] == pattern[j]:
			fail[j] = i + 1
			i += 1
			j += 1
		elif i > 0:
			i = fail[i - 1]
		else:
			j += 1
	return fail
string = input()
n = len(string)
properPrefixArrayL = knuthMorrisonPreprocess(string, n)
properPrefixArray = properPrefixArrayL[-1]
if properPrefixArray == 0:
	print('Just a legend')
	quit()
for i in range(0, n - 1):
	if properPrefixArrayL[i] == properPrefixArrayL[n - 1]:
		print(string[0:properPrefixArrayL[i]])
		quit()
if properPrefixArrayL[properPrefixArrayL[n - 1] - 1] == 0:
	print('Just a legend')
else:
	print(string[0:properPrefixArrayL[properPrefixArrayL[n - 1] - 1]])


def main():
	t = 1
	for z in range(t):
		s = input()
		s = s
		n = len(s)
		z = [0] * n
		l = 0
		r = 0
		for i in range(1, n):
			if r >= i:
				z[i] = min(z[i - l], r - i + 1)
			while z[i] + i < n and s[z[i]] == s[z[i] + i]:
				z[i] += 1
			if i + z[i] - 1 > r:
				l = i
				r = i + z[i] - 1
		ar = [0] * n
		for i in range(n):
			ar[z[i]] += 1
		for i in range(n - 2, -1, -1):
			ar[i] += ar[i + 1]
		ans = -1
		for i in range(1, n):
			if z[i] == n - i and ar[z[i]] > 1:
				ans = i
				break
		if ans == -1:
			print('Just a legend')
		else:
			print(s[ans:])
main()


def prefix(s):
	p = [0]
	for i in s[1:]:
		j = p[-1]
		while j > 0 and s[j] != i:
			j = p[j - 1]
		p.append(j + (s[j] == i))
	return p

def kmp(s, p):
	s = p + '$' + s
	pre = prefix(s)
	o = []
	for i in range(len(p), len(pre)):
		if pre[i] == len(p):
			o.append(i - 2 * len(p))
	return o
s = input()
p = prefix(s)
print(s[:p[-1]] if p[-1] and p.count(p[-1]) > 1 and (len(s) > 2) else s[:p[p[-1] - 1]] if p[p[-1] - 1] else 'Just a legend')


s = input()
n = len(s)
lps = [0] * n
i = 1
length = 0
while i < n:
	if s[i] == s[length]:
		length += 1
		lps[i] = length
		i += 1
	elif length == 0:
		i += 1
	else:
		length = lps[length - 1]
x = lps[-1]
flag = 1
while x:
	for i in range(1, n - 1):
		if lps[i] == x:
			flag = 0
			break
		else:
			flag = 1
	if flag == 0:
		break
	x = lps[x - 1]
print(s[:x]) if flag == 0 else print('Just a legend')


import sys, os, io, time, copy, math
from collections import deque
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def Z_arr(string):
	n = len(string)
	z = [0] * n
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1
	return z

def pi_table(string):
	n = len(string)
	table = [0 for i in range(len(string))]
	curr = 0
	i = 1
	while i < n:
		if string[i] == string[curr]:
			curr += 1
			table[i] = curr
			i += 1
		elif curr != 0:
			curr = table[curr - 1]
		else:
			table[i] = 0
			i += 1
	return table

def main():
	string = input()
	n = len(string)
	table = Z_arr(string)
	table[0] = n
	m = 0
	req = 0
	for i in range(1, n):
		if table[i] + i == n and m >= table[i]:
			if table[i] > req:
				req = table[i]
			elif table[i] > m:
				m = table[i]
		elif table[i] > m:
			m = table[i]
	if req == 0:
		print('Just a legend')
	else:
		print(string[:req])
main()


from sys import stdin, stdout
s = stdin.readline().rstrip()
n = len(s)

def compute_lps(s):
	lps = [0 for i in range(n)]
	Len = 0
	lps[0] = 0
	i = 1
	while i < n:
		if s[i] == s[Len]:
			Len += 1
			lps[i] = Len
			i += 1
		elif Len != 0:
			Len = lps[Len - 1]
		else:
			lps[i] = 0
			i += 1
	return lps

def Longestsubstring(s):
	lps = compute_lps(s)
	if lps[n - 1] == 0:
		return 0
	for i in range(0, n - 1):
		if lps[i] == lps[n - 1]:
			return s[0:lps[i]]
	if lps[lps[n - 1] - 1] == 0:
		return 0
	else:
		return s[0:lps[lps[n - 1] - 1]]
res = Longestsubstring(s)
if res:
	stdout.write(res)
else:
	stdout.write('Just a legend')


import sys
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

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

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 8

def computeLPSArray(pat):
	val = 0
	lps = [0] * len(pat)
	lps[0] = 0
	i = 1
	while i < len(pat):
		if pat[i] == pat[val]:
			val += 1
			lps[i] = val
			i += 1
		elif val != 0:
			val = lps[val - 1]
		else:
			lps[i] = 0
			i += 1
	return lps
s = ri()
dp = computeLPSArray(s)
val = dp[len(s) - 1]
ans = ''
val3 = dp[dp[len(s) - 1] - 1]
maxx = -1
for i in range(len(s) - 2, 0, -1):
	if dp[i] == val and val != 0:
		maxx = val
	elif dp[i] != 0:
		val2 = dp[i]
		if val2 == val3:
			maxx = max(maxx, val2)
ans = s[:maxx]
if maxx != -1:
	print(ans)
else:
	print('Just a legend')


P = input()
m = len(P)
f = [0] * m
j = 1
k = 0
while j < m:
	if P[j] == P[k]:
		f[j] = k + 1
		j += 1
		k += 1
	elif k > 0:
		k = f[k - 1]
	else:
		j += 1
l = f.pop()
if l:
	if l in f:
		print(P[:l])
	elif f[l - 1]:
		print(P[:f[l - 1]])
	else:
		print('Just a legend')
else:
	print('Just a legend')


s = input()

def count_unique_substring(s):
	n = len(s)
	p = 31
	m = 10 ** 9 + 9
	p_pow = [1]
	for i in range(1, n):
		p_pow.append(p_pow[i - 1] * p % m)
	hash = [0]
	count = 0
	flag = 0
	for i in range(n):
		hash.append((hash[i] + (ord(s[i]) - ord('a') + 1) * p_pow[i]) % m)
	for l in range(n, 0, -1):
		c_1 = (hash[l] + m - hash[0]) % m
		c_1 = c_1 * p_pow[n - 1] % m
		c_2 = (hash[n - l + l] + m - hash[n - l]) % m
		c_2 = c_2 * p_pow[l - 1] % m
		if c_1 == c_2:
			for i in range(1, n - l):
				c_h = (hash[i + l] + m - hash[i]) % m
				c_h = c_h * p_pow[n - i - 1] % m
				if c_h == c_2:
					return s[i:i + l]
t = count_unique_substring(s)
if t:
	print(t)
else:
	print('Just a legend')


def kmp_preprocess(s, kmp):
	kmp[0] = 0
	i = 1
	j = 0
	while i < len(s):
		if s[i] == s[j]:
			j += 1
			kmp[i] = j
			i += 1
		elif j != 0:
			j = kmp[j - 1]
		else:
			kmp[i] = 0
			i += 1
s = input()
kmp = [None] * len(s)
kmp_preprocess(s, kmp)
res = None
for i in range(len(s) - 1):
	if kmp[i] == kmp[-1]:
		res = kmp[-1]
		break
if not res:
	if kmp[kmp[-1] - 1] != 0:
		res = kmp[kmp[-1] - 1]
if not res:
	print('Just a legend')
else:
	print(s[:res])


def pf(txt, m, lps):
	l = 0
	lps[0] = 0
	i = 1
	while i < m:
		if txt[i] == txt[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
s = input()
_len = len(s)
if _len == 1 or _len == 2:
	print('Just a legend')
	exit(0)
lps = [0] * _len
pf(s, _len, lps)
val = lps[-1]
if val == 0:
	print('Just a legend')
elif val in lps[:-1]:
	print(s[:val])
elif lps[val - 1] == 0:
	print('Just a legend')
elif lps[val - 1]:
	print(s[:lps[val - 1]])
else:
	print('Just a legend')


import sys
I = lambda : int(input())
readline = lambda : sys.stdin.readline().strip()
RM = readlist = lambda x=int: map(x, readline().split())

def prefix_function(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j > 0 and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi
s = readline()
pi = prefix_function(s)
pis = set(pi[:-1])
j = pi[-1]
while j > 0 and (not j in pis):
	j = pi[j - 1]
print(s[:j] if j > 0 else 'Just a legend')
quit()
s = readline()
n = len(s)
(ppow, base, p, m) = ([0] * int(n + 5), 1, 31, int(1000000000.0 + 7))
for i in range(n + 5):
	(ppow[i], base) = (base, base * p % m)
hashp = [0] * n
hashp[0] = ord(s[0]) - 96
for ind in range(1, n):
	hashp[ind] = (hashp[ind - 1] + (ord(s[ind]) - 96) * ppow[ind]) % m
stop = False
for l in range(n - 1, 0, -1):
	prefix_hash = hashp[l - 1] * ppow[n + 1] % m
	suffix_hash = (hashp[n - 1] - hashp[n - l - 1]) * ppow[l + 1] % m
	if suffix_hash != prefix_hash:
		continue
	for t in range(1, n - l):
		temp_hash = (hashp[t + l - 1] - hashp[t - 1]) * ppow[n - t + 1] % m
		if temp_hash == suffix_hash:
			print(s[t:t + l])
			stop = True
			break
	if stop:
		break
else:
	print('Just a legend')


import sys
import math
import collections
import heapq
import decimal
input = sys.stdin.readline
s = input()
n = len(s) - 1
l = [0] * n
k = 0
i = 1
while i < n:
	if s[i] == s[k]:
		k += 1
		l[i] = k
		i += 1
	elif k != 0:
		k = l[k - 1]
	else:
		l[i] = 0
		i += 1
if l[n - 1] == 0:
	print('Just a legend')
else:
	k = -1
	for i in range(n - 1):
		if l[i] == l[n - 1]:
			k = i
			break
	if k == -1:
		if l[l[n - 1] - 1] == 0:
			print('Just a legend')
		else:
			print(s[:l[l[n - 1] - 1]])
	else:
		print(s[:l[k]])


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()
res = ['YES', 'NO']

def create_z(s):
	z = [0] * len(s)
	x = y = 0
	for i in range(1, len(s)):
		z[i] = max(0, min(z[i - x], y - i + 1))
		while i + z[i] < len(s) and s[z[i]] == s[i + z[i]]:
			x = i
			y = i + z[i]
			z[i] += 1
	return z
a = lsi()
n = len(a)
z = create_z(a)
mx = max(z)
pos = z.index(mx)
m = 0
for i in range(max(pos + 1, n - mx), n):
	if z[i] == n - i:
		m = n - i
		break
if m < 1:
	print('Just a legend')
else:
	print(*a[:m], sep='')


def pre(s):
	n = len(s)
	l = 0
	lps = [0] * n
	for i in range(1, n):
		j = lps[i - 1]
		while j > 0 and s[i] != s[j]:
			j = lps[j - 1]
		if s[i] == s[j]:
			j += 1
		lps[i] = j
	for i in range(1, n):
		if lps[i] == i + 1:
			lps[i] -= 1
	return lps
from collections import defaultdict

def solve():
	s = input()
	lps1 = pre(s)
	n = len(s)
	ans = 0
	(z2, z3) = (lps1[n - 1], lps1[lps1[n - 1] - 1])
	final = -1
	for i in range(n):
		z1 = lps1[i]
		if i != n - 1:
			if (z1 == z2 or z1 == z3) and z1 != 0:
				ans = max(ans, z1)
				if ans == z1:
					final = i
	if final == -1:
		print('Just a legend')
	else:
		z = s[final - ans + 1:final + 1]
		print(z)
solve()


(s, j, next) = (input(), -1, [-1])
for c in s:
	while j != -1 and c != s[j]:
		j = next[j]
	j += 1
	next.append(j)
a = next[-1]
if next.count(a) < 2:
	a = next[a]
print(s[:a] if a > 0 else 'Just a legend')


def Zarr(pat, text):
	s = pat + '/' + text
	z = [0 for i in range(len(s))]
	L = 0
	R = 0
	n = len(s)
	for i in range(1, len(s)):
		if i > R:
			L = R = i
			while R < n and s[R - L] == s[R]:
				R += 1
			z[i] = R - L
			R -= 1
		elif z[i - L] + i <= R:
			z[i] = z[i - L]
		else:
			L = i
			while R < n and s[R - L] == s[R]:
				R += 1
			z[i] = R - L
			R -= 1
	dp = [0 for i in range(1000006)]
	ans = -1
	for i in range(len(pat) + 2, len(s)):
		dp[z[i]] += 1
	for i in range(len(dp) - 2, -1, -1):
		dp[i] += dp[i + 1]
	for i in range(len(pat) + 2, len(s)):
		if i + z[i] == len(s) and dp[z[i]] > 1 and (z[i] > ans):
			ans = z[i]
	if ans == -1:
		print('Just a legend')
	else:
		print(pat[0:ans])
from sys import stdin
text = stdin.readline().strip()
Zarr(text, text)


from sys import stdin, stdout, stderr

def lcp(string):
	l = len(string)
	lis = [0] * l
	j = 1
	k = 0
	while j < l:
		if string[j] == string[k]:
			lis[j] = k + 1
			j += 1
			k += 1
		elif k > 0:
			k = lis[k - 1]
		else:
			j += 1
	return lis
string = input()
lis = lcp(string)
if lis[-1] == 0:
	print('Just a legend')
	exit()
for i in range(len(string) - 1):
	if lis[i] != 0 and lis[i] == lis[-1]:
		print(string[:lis[-1]])
		exit()
l = lis[-1]
if l == 0:
	print('Just a legend')
	exit()
l = lis[lis[-1] - 1]
if l:
	print(string[:l])
	exit()
print('Just a legend')


import os
import sys
from io import BytesIO, IOBase
mod = 10 ** 9 + 7

def getZarr(string, z):
	n = len(string)
	(l, r, k) = (0, 0, 0)
	for i in range(1, n):
		if i > r:
			(l, r) = (i, i)
			while r < n and string[r - l] == string[r]:
				r += 1
			z[i] = r - l
			r -= 1
		else:
			k = i - l
			if z[k] < r - i + 1:
				z[i] = z[k]
			else:
				l = i
				while r < n and string[r - l] == string[r]:
					r += 1
				z[i] = r - l
				r -= 1

def main():
	s = input()
	n = len(s)
	z = [0] * n
	getZarr(s, z)
	ans = 0
	flag = 0
	for i in range(1, n):
		if i + z[i] == n:
			if z[i] <= flag:
				ans = max(ans, z[i])
		flag = max(flag, z[i])
	if ans == 0:
		print('Just a legend')
	else:
		print(s[:ans])

def nouse0():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse1():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse2():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
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 nouse3():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse4():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')

def nouse5():
	a = 420
	b = 420
	print(f'i am nitish{(a + b) // 2}')
main()


s = input()
count = -1
arr = [-1]
for x in s:
	while count != -1 and s[count] != x:
		count = arr[count]
	count = count + 1
	arr.append(count)
le = arr[-1]
if arr.count(le) < 2:
	le = arr[le]
if le > 0:
	print(s[:le])
else:
	print('Just a legend')


from sys import stdin, stdout
from collections import defaultdict, Counter
from bisect import bisect
import math

def ps(s):
	n = len(s)
	pre = [0 for i in range(n)]
	j = 0
	i = 1
	while i < n:
		if s[j] == s[i]:
			j += 1
			pre[i] = j
			i += 1
		elif j != 0:
			j = pre[j - 1]
		else:
			pre[i] = 0
			i += 1
	return pre
I = stdin.readline
s = I().rstrip()
arr = ps(s)
pn = arr[-1]
flag = 0
if pn != 0:
	for i in range(len(s) - 1):
		if arr[i] == pn:
			flag = 1
			break
if flag == 1:
	print(s[0:pn])
else:
	pn = arr[pn - 1]
	if pn != 0:
		flag = 1
	if flag == 1:
		print(s[0:pn])
	else:
		print('Just a legend')


def prefixfunction(txt, m, lps):
	l = 0
	lps[0] = 0
	i = 1
	while i < m:
		if txt[i] == txt[l]:
			l += 1
			lps[i] = l
			i += 1
		elif l != 0:
			l = lps[l - 1]
		else:
			lps[i] = 0
			i += 1
s = input().strip()
longitud = len(s)
if longitud == 1 or longitud == 2:
	print('Just a legend')
	exit(0)
lps = [0] * longitud
prefixfunction(s, longitud, lps)
val = lps[-1]
if val == 0:
	print('Just a legend')
elif val in lps[:-1]:
	print(s[:val])
elif lps[val - 1] == 0:
	print('Just a legend')
elif lps[val - 1]:
	print(s[:lps[val - 1]])
else:
	print('Just a legend')


s = input()
n = len(s)
p = [0] * n
for i in range(1, n):
	cur = p[i - 1]
	while s[i] != s[cur] and cur > 0:
		cur = p[cur - 1]
	if s[i] == s[cur]:
		p[i] = cur + 1
if p[-1] == 0:
	print('Just a legend')
else:
	sa = p[:-1]
	if p[-1] not in sa:
		if p[p[-1] - 1] == 0:
			print('Just a legend')
		else:
			print(s[:p[p[-1] - 1]])
	else:
		print(s[:p[-1]])


def solve(s):
	n = len(s)
	lps = [0] * n
	count = [0] * (n + 1)
	for i in range(1, n):
		l = lps[i - 1]
		while l > 0 and s[i] != s[l]:
			l = lps[l - 1]
		if s[i] == s[l]:
			l += 1
		lps[i] = l
		if i < n - 1:
			count[l] += 1
	l = lps[n - 1]
	while l > 0:
		if count[l] > 0:
			return s[:l]
		l = lps[l - 1]
s = input()
res = solve(s)
if res:
	print(res)
else:
	print('Just a legend')


import os
from sys import stdin, stdout

def getTp(w):
	m = len(w)
	tp = [0 for i in range(m)]
	j = 0
	for i in range(1, m):
		while j > 0 and w[i] != w[j]:
			j = tp[j - 1]
		if w[i] == w[j]:
			j += 1
			tp[i] = j
	return tp

def kmp(s, w):
	tp = getTp(w)
	(n, m) = (len(s), len(w))
	j = 0
	for i in range(1, n - 1):
		while j > 0 and s[i] != w[j]:
			j = tp[j - 1]
		if s[i] == w[j]:
			if j == m - 1:
				return True
			else:
				j += 1
	return False

def solve(tc):
	s = stdin.readline().strip()
	tp = getTp(s)
	p = len(s) - 1
	if tp[p] == 0:
		print('Just a legend')
		return
	while tp[p] > 0:
		w = s[:tp[p]]
		if kmp(s, w):
			print(w)
			return
		p = tp[p - 1]
	print('Just a legend')
tcs = 1
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


import bisect

def ZAlgo(s):
	z = [0] * len(s)
	left = right = 0
	for k in range(1, len(s)):
		if k > right:
			left = right = k
			while right < len(s) and s[right] == s[right - left]:
				right += 1
			z[k] = right - left
			right -= 1
		else:
			k1 = k - left
			if z[k1] < right - k + 1:
				z[k] = z[k1]
			else:
				left = k
				while right < len(s) and s[right] == s[right - left]:
					right += 1
				z[k] = right - left
				right -= 1
	return z
pat = input()
M = len(pat)
z = ZAlgo(pat)
c = 1
suf = []
count = 0
m = 0
for i in range(len(z)):
	if z[i] > 0:
		p = i
		break
suffix = {}
for i in range(M - 1, -1, -1):
	if z[i] == c and z[i] < len(pat) - p:
		suf.append(z[i])
		suffix[z[i]] = i
	c += 1
suf = sorted(suf)
if len(suf) > 1:
	for i in range(len(z)):
		if z[i] not in suffix or suffix[z[i]] != i:
			m = max(m, bisect.bisect_right(suf, z[i]))
	print(pat[:suf[m - 1]])
elif len(suf) == 1:
	for i in z:
		if i >= suf[0]:
			count += 1
	if count >= 2:
		print(pat[:suf[0]])
	else:
		print('Just a legend')
else:
	print('Just a legend')


def KMPS(pat, txt, lps):
	M = len(pat)
	N = len(txt)
	j = 0
	i = 0
	while i < N:
		if pat[j] == txt[i]:
			i += 1
			j += 1
		if j == M:
			return 1
			j = lps[j - 1]
		elif i < N and pat[j] != txt[i]:
			if j != 0:
				j = lps[j - 1]
			else:
				i += 1
	return 0
s = input()
s = s.lower()
n = len(s)
pi = [0] * n
m = 0
i = 1
while i < n:
	if s[i] == s[m]:
		m += 1
		pi[i] = m
		i += 1
	elif m:
		m = pi[m - 1]
	else:
		pi[i] = 0
		i += 1
cr = pi[n - 1]
if cr == 0:
	print('Just a legend')
else:
	an = s[n - cr:]
	ix = 1
	for i in range(n):
		if pi[i]:
			ix = i
			break
	if pi.count(len(an)) < 2:
		an = an[ix:]
		t = len(an)
		if an:
			if an == s[:t] and an == s[n - t:]:
				pi = [0] * t
				m = 0
				i = 1
				while i < t:
					if an[i] == an[m]:
						m += 1
						pi[i] = m
						i += 1
					elif m:
						m = pi[m - 1]
					else:
						pi[i] = 0
						i += 1
				if not KMPS(an, s[1:n - 1], pi):
					an = ''
			else:
				an = ''
	if an:
		print(an)
	else:
		print('Just a legend')


import os
from sys import stdin, stdout

def getPrefixFunction(w):
	m = len(w)
	pf = [0 for i in range(m)]
	j = 0
	for i in range(1, m):
		while j > 0 and w[i] != w[j]:
			j = pf[j - 1]
		if w[i] == w[j]:
			j += 1
			pf[i] = j
	return pf

def solve(tc):
	s = stdin.readline().strip()
	n = len(s)
	pf = getPrefixFunction(s)
	if pf[n - 1] == 0:
		print('Just a legend')
		return
	for i in range(1, n - 1):
		if pf[i] == pf[n - 1]:
			print(s[:pf[n - 1]])
			return
	k = pf[pf[n - 1] - 1]
	if k > 0:
		print(s[:k])
		return
	print('Just a legend')
tcs = 1
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


import sys
import bisect
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
gp = []
cost = []
dp = []
mx = []

def dfs(root, par):
	global gp, cost, dp
	dp[root] = cost[root]
	for i in gp[root]:
		if i == par:
			continue
		dfs(i, root)
		dp[root] = max(dp[root], dp[i])

def z_algo(ar):
	n = len(ar)
	ans = [0] * n
	(l, r) = (0, 0)
	ans[0] = n
	for i in range(1, n):
		if i <= r:
			ans[i] = min(r - i + 1, ans[i - l])
		while i + ans[i] < n and ar[ans[i]] == ar[i + ans[i]]:
			ans[i] += 1
		if i + ans[i] - 1 > r:
			l = i
			r = i + ans[i] - 1
	return ans

def hnbhai():
	s = sa()
	n = len(s)
	z = z_algo(s)
	count = [0] * (n + 1)
	mark = [0] * (n + 1)
	tot = 0
	for i in range(n - 1, -1, -1):
		if z[i] + i == n:
			mark[z[i]] = 1
			tot += 1
	for i in range(n):
		count[z[i]] += 1
	for i in range(n - 1, -1, -1):
		count[i] += count[i + 1]
	for i in range(n - 1, -1, -1):
		if mark[i] and count[i] > 2:
			print(s[:i])
			return
	print('Just a legend')
for _ in range(1):
	hnbhai()


s = input()
t = [0]
(i, length) = (1, 0)
while i < len(s):
	if s[i] == s[length]:
		length += 1
		t.append(length)
		i += 1
	elif length == 0:
		t.append(length)
		i += 1
	else:
		length = t[length - 1]
if t[-1] != 0:
	if len([a for a in t if a == t[-1]]) >= 2:
		print(s[:t[-1]])
	else:
		q = t[t[-1] - 1]
		if q != 0:
			print(s[:q])
		else:
			print('Just a legend')
else:
	print('Just a legend')


pat = input()
m = len(pat)
lps = [0 for i in range(m)]
(j, i) = (0, 1)
while i < m:
	if pat[i] == pat[j]:
		j += 1
		lps[i] = j
		i += 1
	elif j == 0:
		lps[i] = 0
		i += 1
	else:
		j = lps[j - 1]
req = lps[-1]
if lps[-1] != 0 and lps.count(req) > 1:
	print(pat[:req])
elif lps[-1] != 0 and lps[lps[-1] - 1] != 0:
	print(pat[:lps[lps[-1] - 1]])
else:
	print('Just a legend')


import sys, math
readline = sys.stdin.readline
s = readline().split()[0]

def z_Func(s):
	n = len(s)
	(l, r) = (0, 0)
	z = [0 for _ in range(n)]
	for i in range(1, n):
		if i <= r:
			z[i] = min(r - i + 1, z[i - l])
		while i + z[i] < n and s[z[i]] == s[i + z[i]]:
			z[i] += 1
		if i + z[i] - 1 > r:
			(l, r) = (i, i + z[i] - 1)
	return z
tp = z_Func(s)
j = 0
m = 0
for i in range(len(tp)):
	if tp[i] == len(tp) - i and tp[i] <= m:
		j = i
		break
	m = max(m, tp[i])
if j:
	print(s[:len(tp) - j])
else:
	print('Just a legend')


def buildZ(S):
	(L, R) = (0, 0)
	n = len(S)
	Z = [0] * n
	for i in range(1, n):
		if i > R:
			L = R = i
			while R < n and S[R - L] == S[R]:
				R += 1
			Z[i] = R - L
			R -= 1
		else:
			k = i - L
			if Z[k] < R - i + 1:
				Z[i] = Z[k]
			else:
				L = i
				while R < n and S[R - L] == S[R]:
					R += 1
				Z[i] = R - L
				R -= 1
	return Z
S = input().strip()
Z = buildZ(S)
(maxz, res) = (0, 0)
n = len(S)
for i in range(1, n):
	if Z[i] == n - i and maxz >= n - i:
		res = n - i
		break
	maxz = max(maxz, Z[i])
if res > 0:
	print(S[0:res])
else:
	print('Just a legend')


from sys import stdin

def prefix_calculator(string):
	p = [0] * len(string)
	j = 0
	for i in range(1, len(string)):
		while j > 0 and string[i] != string[j]:
			j = p[j - 1]
		if string[i] == string[j]:
			j += 1
		p[i] = j
	return p
string = stdin.readline().strip()
if len(string) <= 2:
	print('Just a legend')
	exit()
p = prefix_calculator(string)
if p[-1] == 0:
	print('Just a legend')
else:
	for i in range(1, len(p) - 1):
		if p[i] == p[-1]:
			print(string[0:p[-1]])
			break
	else:
		if p[p[-1] - 1] != 0:
			print(string[0:p[p[-1] - 1]])
		else:
			print('Just a legend')


from sys import stdin, stdout

def z_function(s):
	z = [0] * len(s)
	(l, r) = (0, 0)
	for i in range(1, len(s)):
		z[i] = max(0, min(z[i - l], r - i))
		while z[i] + i < len(s) and s[z[i]] == s[z[i] + i]:
			z[i] += 1
		if i + z[i] - 1 > r:
			(l, r) = (i, i + z[i])
	return z

def prefix_function(s):
	v = [0] * len(s)
	for i in range(1, len(s)):
		k = v[i - 1]
		while k > 0 and s[k] != s[i]:
			k = v[k - 1]
		if s[k] == s[i]:
			k = k + 1
		v[i] = k
	return v

def build_hash(s):
	global hs, pw, q, p
	n = len(s)
	hs = [0] * (n + 1)
	pw = [1] * (n + 1)
	for i in range(n):
		hs[i + 1] = (hs[i] * q + ord(s[i])) % p
		pw[i + 1] = pw[i] * q % p

def get_hash(a, b):
	return (hs[b] - hs[a] * pw[b - a]) % p
s = input()
p = prefix_function(s)
if p.count(p[-1]) > 1 and p[-1] > 0:
	print(s[:p[-1]])
else:
	q = p[p[-1] - 1]
	if q > 0:
		print(s[:q])
	else:
		print('Just a legend')