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


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


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


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


import sys
input = sys.stdin.readline
import collections as cc
s = input().strip()
n = len(s)
temp = cc.Counter(s)
pre = [0] * n
i = 1
j = 0
lst = -1
while i < n:
	if s[i] == s[j]:
		j += 1
		pre[i] = j
		i += 1
	elif j:
		j = pre[j - 1]
	else:
		pre[i] = 0
		i += 1
pre[0] = 0
temp = pre[-1]
if pre[-1] != 0 and pre.count(temp) >= 2:
	print(''.join(s[:temp]))
elif pre[-1] != 0 and pre[pre[-1] - 1] > 0:
	print(''.join(s[:pre[pre[-1] - 1]]))
else:
	print('Just a legend')


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List
from functools import lru_cache
s = input()

def solve_hash(s):
	print('starting...')
	t0 = time.time()
	MOD = 10 ** 9 + 7
	N = len(s)
	(ha, hb) = (0, 0)
	possible = []
	(x, oa) = (1, ord('a'))
	w = [ord(v) - oa for v in s]
	for (i, v) in enumerate(w):
		ha = (ha * 26 + v) % MOD
		hb = (w[N - i - 1] * x + hb) % MOD
		x = x * 26 % MOD
		if ha == hb:
			possible.append(i + 1)
	print(time.time() - t0)

	def check(m):
		l = possible[m]
		t = s.find(s[:l], 1)
		return t > 0 and t != N - l
	(lo, hi) = (0, len(possible))
	while lo <= hi:
		m = (lo + hi) // 2
		if check(m):
			lo = m + 1
		else:
			hi = m - 1
	print(time.time() - t0)

def solve_zscore(s):
	(z, maxl) = ([0] * len(s), 0)
	(i, l, r, n) = (1, 0, 0, len(s))
	while i < n:
		if i <= r:
			z[i] = min(r - i + 1, z[i - l])
		while i + z[i] < n and s[z[i]] == s[z[i] + i]:
			z[i] += 1
		if i + z[i] - 1 > r:
			(l, r) = (i, i + z[i] - 1)
		if i + z[i] == n:
			if z[i] <= maxl:
				print(s[:z[i]])
				return
		maxl = max(maxl, z[i])
		i += 1
	print('Just a legend')
solve_zscore(s)


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


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


import os
from io import BytesIO, IOBase
import sys

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

def main():
	s = input().rstrip()
	r = s[:pie(s[:max(pie(s + '#' + s[1:len(s) - 1])[len(s):])] + '#' + s)[-1]]
	print(r if r else '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]])


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


import sys
input = sys.stdin.readline

def zalgo(s=''):
	L = R = 0
	n = len(s)
	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:
			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
s = input().rstrip()
z = [0] * len(s)
zalgo(s)
ret = maxz = 0
n = len(z)
for i in range(n):
	if z[i] == n - i and maxz >= n - i:
		ret = i
		break
	maxz = max(maxz, z[i])
print(s[ret:] if ret else 'Just a legend')


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


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


def KMP_table(s):
	ret = [0]
	for i in range(0, len(s)):
		j = i
		while True:
			if j == 0:
				ret.append(0)
				break
			if s[ret[j]] == s[i]:
				ret.append(ret[j] + 1)
				break
			j = ret[j]
	return ret

def strictly_increasing(L):
	return all((x < y for (x, y) in zip(L, L[1:])))
INPUT = input().rstrip()
T = KMP_table(INPUT)
longest_prefix = T[len(T) - 1]
isSamePrefix = T[longest_prefix]
for i in range(1, len(INPUT) - 1):
	if T[i] == longest_prefix:
		isSamePrefix = longest_prefix
if isSamePrefix == 0:
	print('Just a legend')
else:
	print(INPUT[0:isSamePrefix])


s = input()
com = -1
cn = [-1]
for c in s:
	while com != -1 and c != s[com]:
		com = cn[com]
	com += 1
	cn.append(com)
lenth = cn[-1]
if cn.count(lenth) < 2:
	lenth = cn[lenth]
print(s[:lenth] if lenth > 0 else '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])


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


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


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


__author__ = 'Utena'

def compute_kmp_fail(P):
	m = len(P)
	fail = [0] * m
	j = 1
	k = 0
	while j < m:
		if P[j] == P[k]:
			fail[j] = k + 1
			j += 1
			k += 1
		elif k > 0:
			k = fail[k - 1]
		else:
			j += 1
	return fail
t = input()
f = compute_kmp_fail(t)
if f[-1] != 0:
	for i in range(0, len(t) - 1):
		if f[i] == f[-1]:
			print(t[:f[-1]])
			exit(0)
	if f[f[-1] - 1] > 0:
		print(t[:f[f[-1] - 1]])
	else:
		print('Just a legend')
else:
	print('Just a legend')


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


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


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


import math
import sys
from bisect import bisect_right, bisect_left, insort_right
from collections import Counter, defaultdict
from heapq import heappop, heappush
from itertools import accumulate, permutations, combinations
from sys import stdout
R = lambda : map(int, input().split())
s = input()
n = len(s)
z = [0] * n
(l, r) = (0, 0)
for i in range(1, n):
	if i > r:
		k = i
		while k < n and s[k] == s[k - i]:
			k += 1
		(l, r) = (i, k - 1)
		z[l] = r - l + 1
	elif i + z[i - l] <= r:
		z[i] = z[i - l]
	else:
		j = r + 1
		while j < n and s[j] == s[j - i]:
			j += 1
		(l, r) = (i, j - 1)
		z[i] = r - l + 1
sz = sorted(z[1:])
for l in range(n - 1, 0, -1):
	if z[n - l] == l and len(sz) - bisect_left(sz, l) >= 2:
		print(s[:l])
		exit(0)
print('Just a legend')


def kmpfailureFunc(P):
	lP = len(P)
	kmptable = [0, 0]
	k = 0
	i = 1
	while i < lP:
		if P[i] == P[k]:
			kmptable.append(k + 1)
			i += 1
			k += 1
		elif k > 0:
			k = kmptable[k]
		else:
			kmptable.append(0)
			i += 1
	return kmptable
s = input().strip()
L = len(s)
l = L - 2
kmpfailFunc = kmpfailureFunc(s)
longest = kmpfailFunc[-1]
if longest > 0:
	i = 0
	while i < L:
		if longest == kmpfailFunc[i]:
			print(s[:longest])
			break
		i += 1
	else:
		longest = kmpfailFunc[longest]
		if longest:
			print(s[:longest])
		else:
			print('Just a legend')
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 getLPS(s1, l):
	lps = [0] * l
	i = 1
	j = 0
	while i < l:
		if s1[i] == s1[j]:
			lps[i] = j + 1
			j += 1
			i += 1
		elif j != 0:
			j = lps[j - 1]
		else:
			lps[i] = 0
			i += 1
	return lps
s = input()
n = len(s)
arr = getLPS(s, n)
lenth = arr[-1]
if arr.count(lenth) == 1:
	lenth = arr[lenth - 1]
if lenth > 0:
	print(s[:lenth])
else:
	print('Just a legend')


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


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


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


s = input()
n = len(s)
p = [0] * (n + 1)
mark = [False] * (n + 1)
for i in range(1, n):
	j = p[i - 1]
	while j > 0 and s[i] != s[j]:
		j = p[j - 1]
	if s[i] == s[j]:
		j += 1
	p[i] = j
	if i < n - 1:
		mark[j] = True
j = p[n - 1]
if j > 0 and mark[j] != True:
	j = p[j - 1]
print(s[0:j] if j > 0 else '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)


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


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)


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


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)


(s, k, lps) = (input(), -1, [-1])
for c in s:
	while k != -1 and s[k] != c:
		k = lps[k]
	k += 1
	lps.append(k)
lenth = lps[-1]
if lps.count(lenth) < 2:
	lenth = lps[lenth]
print(s[:lenth] if lenth > 0 else '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()


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


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


def compute_KMP_fail(P):
	m = len(P)
	fail = [0] * m
	i = 1
	j = 0
	while i < m:
		if P[i] == P[j]:
			fail[i] = j + 1
			i += 1
			j += 1
		elif j > 0:
			j = fail[j - 1]
		else:
			fail[i] = 0
			i += 1
	return fail
s = str(input())
F = compute_KMP_fail(s)
if F[-1] == 0:
	print('Just a legend')
else:
	for i in range(0, len(s) - 1):
		if F[i] == F[-1]:
			print(s[0:F[-1]])
			exit()
	else:
		if F[F[-1] - 1] > 0:
			print(s[0:F[F[-1] - 1]])
		else:
			print('Just a legend')


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 = lps[lps[-1] - 1]
if x <= 0:
	x = -1
k = lps[-1]
if lps.count(k) > 1 and k > 0:
	pass
else:
	k = -1
k = max(x, k)
if k > 0:
	print(string[:k])
else:
	print('Just a legend')


string = input()
n = len(string)
failure = [0] * n
i = 1
j = 0
while i < n:
	if string[i] == string[j]:
		failure[i] = j + 1
		i += 1
		j += 1
	elif j > 0:
		j = failure[j - 1]
	else:
		failure[i] = 0
		i += 1
tem = failure.pop()
if tem:
	if tem in failure:
		print(string[:tem])
	elif failure[tem - 1]:
		print(string[:failure[tem - 1]])
	else:
		print('Just a legend')
else:
	print('Just a legend')


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


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 kmp_diagnose(pattern):
	plen = len(pattern)
	longest_prefix = [0 for _ in range(plen)]
	for i in range(1, plen):
		longest_prefix[i] = longest_prefix[i - 1]
		while longest_prefix[i] >= 0:
			if pattern[i] == pattern[longest_prefix[i]]:
				longest_prefix[i] += 1
				break
			if longest_prefix[i] == 0:
				break
			longest_prefix[i] = longest_prefix[longest_prefix[i] - 1]
	return longest_prefix

def longest_midfix(string):
	(n, longest_prefix) = (len(string), kmp_diagnose(string))
	allowed_midfix_length = set()
	max_allowed = longest_prefix[n - 1]
	while max_allowed > 0:
		allowed_midfix_length.add(max_allowed)
		max_allowed = longest_prefix[max_allowed - 1]
	longest = 0
	for i in range(n - 1):
		if longest_prefix[i] in allowed_midfix_length:
			longest = max(longest, longest_prefix[i])
	if longest > 0:
		return string[:longest]
	return 'Just a legend'
s = input()
print(longest_midfix(s))


from collections import Counter
import sys

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

def ilele():
	return map(int, sys.stdin.readline().strip().split())

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

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
text = input()
n = len(text)
lps = [0] * n
computeLPSArray(text, n, lps)
t = lps[-1]
if lps.count(t) < 2:
	t = lps[t - 1]
print(text[:t] if t > 0 else 'Just a legend')


def subhash(l, r):
	global un_h
	global pw
	big = un_h[r]
	small = 0
	if l > 0:
		small = un_h[l - 1] * pw[r - l + 1] % mod
	return (big - small + mod) % mod

def HASH(st):
	global p
	global mod
	un_h = [0] * len(st)
	un_h[0] = ord(st[0])
	for i in range(1, len(st)):
		un_h[i] = (un_h[i - 1] * p + ord(st[i])) % mod
	return un_h

def hash2(st, hash3=0):
	global p
	global mod
	for i in range(len(st)):
		hash3 = (hash3 * p + ord(st[i])) % mod
	return hash3
p = 29
mod = 10 ** 9 + 7
str1 = input()
pw = [1] * (len(str1) + 1)
for i in range(1, len(pw)):
	pw[i] = pw[i - 1] * p % mod
un_h = HASH(str1)
ans = 'Just a legend'
prefs = [0]
for z in range(0, len(str1) - 2):
	if subhash(0, z) == subhash(len(str1) - 1 - z, len(str1) - 1):
		prefs.append(z + 1)
r = len(prefs)
l = 0
while r - l != 1:
	m = (r + l) // 2
	hash1 = subhash(0, prefs[m] - 1)
	i = 1
	j = prefs[m]
	t = False
	while j < len(str1) - 1:
		H = subhash(i, j)
		if H == hash1:
			l = m
			t = True
			break
		i += 1
		j += 1
	if t == False:
		r = m
if prefs[l] == 0:
	print(ans)
else:
	print(str1[:prefs[l]])


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


def find_fail(P):
	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
	return f
s = input()
fail = [0] * len(s)
fail = find_fail(s)
maximam = fail.pop()
if maximam == 0:
	print('Just a legend')
elif maximam in fail:
	print(s[:maximam])
elif fail[maximam - 1]:
	print(s[:fail[maximam - 1]])
else:
	print('Just a legend')


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(n - 1, 0, -1):
	if z[i] == n - i:
		for j in range(1, i):
			if z[j] >= z[i]:
				l = z[i]
				break
if l > 0:
	print(string[0:l])
else:
	print('Just a legend')


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


s = input()
n = len(s)
m = [0] * (n + 1)
z = [0] * (n + 1)
l = 0
r = 0
flag = 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
	m[z[i]] += 1
for i in range(n - 1, -1, -1):
	m[i] += m[i + 1]
for i in range(1, n):
	if i + z[i] == n and m[z[i]] > 1:
		print(s[0:z[i]])
		flag = 1
		break
if flag == 0:
	print('Just a legend')


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

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

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

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def prefixes(string):
	n = len(string)
	table = [0] * n
	(i, j) = (0, 1)
	while j < n:
		if string[i] == string[j]:
			i += 1
			table[j] = i
			j += 1
		elif i > 0:
			i = table[i - 1]
		else:
			j += 1
	return table
s = data()
n = len(s)
arr = prefixes(s)
index = 0
for i in range(n - 1):
	if not arr[i] and arr[i + 1]:
		index = i + 1
		break
if 0 not in arr[index:]:
	(t1, t2) = (s[:n - 1 - index], s[:arr[-1] - index])
	if s.endswith(t1) and len(t1) > 0:
		out(t1)
		exit()
	elif s.endswith(t2) and len(t2) > 0:
		out(t2)
		exit()
d = dd(list)
for i in range(n):
	d[arr[i]].append(i)
t = n - 1
for i in range(n - 1, -1, -1):
	if not arr[i]:
		t = i + 1
		break
if 0 in d.keys():
	del d[0]
answer = -1
for index in d.keys():
	if len(d[index]) <= 1:
		continue
	if t <= d[index][-1] < n:
		answer = max(answer, index)
if answer != -1:
	if s.endswith(s[:answer]):
		out(s[:answer])
		exit()
out('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')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

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

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 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 pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def z_algorithm(s):
	sl = len(s)
	a = [0] * sl
	a[0] = sl
	i = 1
	j = 0
	while i < sl:
		while i + j < sl and s[j] == s[i + j]:
			j += 1
		a[i] = j
		if j == 0:
			i += 1
			continue
		k = 1
		while i + k < sl and k + a[k] < j:
			a[i + k] = a[k]
			k += 1
		i += k
		j -= k
	return a

def main():
	t = 1
	rr = []
	for _ in range(t):
		s = S()
		sl = len(s)
		z = z_algorithm(s)
		r = 0
		u = 0
		for i in range(1, len(s)):
			t = z[i]
			if i + t == sl and u >= t:
				r = t
				break
			if u < t:
				u = t
		rr.append(IF(r == 0, 'Just a legend', s[:r]))
	return JA(rr, '\n')
print(main())


s = input()
a = [-1]
j = -1
for i in range(1, len(s)):
	while j > -1 and s[j + 1] != s[i]:
		j = a[j]
	if s[j + 1] == s[i]:
		j += 1
	a.append(j)
j = a[-1]
b = set(a[:-1])
while j > -1 and (not j in b):
	j = a[j]
print('Just a legend' if j == -1 else s[:j + 1])


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)


from sys import stdin, stdout

def read():
	return stdin.readline().rstrip()

def solve(s, z):
	maxi = 0
	pos = 0
	for i in range(1, len(s)):
		rev_idx = len(s) - i
		if z[i] == rev_idx and maxi >= rev_idx:
			pos = i
			break
		maxi = max(maxi, z[i])
	else:
		return False
	return s[pos:]

def zf(s):
	n = len(s)
	z = [0] * n
	L = R = 0
	for i in range(1, n):
		if i <= R:
			z[i] = min(z[i - L], R - i + 1)
		while i + z[i] < n and s[i + z[i]] == s[z[i]]:
			z[i] += 1
		if i + z[i] - 1 > R:
			(L, R) = (i, i + z[i] - 1)
	return solve(s, z)
password = zf(read())
stdout.write('Just a legend\n' if not password else password + '\n')


def LPS(pat):
	M = len(pat)
	j = 0
	lps = [0 for i in range(M)]
	i = 1
	while i < M:
		if pat[i] == pat[j]:
			j += 1
			lps[i] = j
			i += 1
		elif j != 0:
			j = lps[j - 1]
		else:
			lps[i] = 0
			i += 1
	return lps
s = input()
lps = LPS(s)
ans = lps[-1]
ct = 0
for i in range(len(s)):
	if lps[i] == ans:
		ct += 1
if ct < 2:
	ans = lps[ans - 1]
if ans > 0:
	print(s[:ans])
else:
	print('Just a legend')


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


def KMP_pre(texte, longueur):
	M = [0 for i in range(longueur)]
	index_deb = 0
	M[0] = 0
	i = 1
	while i < longueur:
		if texte[i] == texte[index_deb]:
			index_deb += 1
			M[i] = index_deb
			i += 1
		elif index_deb != 0:
			index_deb = M[index_deb - 1]
		else:
			M[i] = 0
			i += 1
	return M

def KMP_algo(texte, longueur):
	M = KMP_pre(texte, longueur)
	if M[longueur - 1] == 0:
		return -1
	for i in range(0, longueur - 1):
		if M[i] == M[longueur - 1]:
			return texte[0:M[i]]
	if M[M[longueur - 1] - 1] == 0:
		return -1
	else:
		return texte[0:M[M[longueur - 1] - 1]]

def main(texte):
	res = KMP_algo(texte, len(texte))
	if res == -1:
		print('Just a legend')
	else:
		print(res)
main(input())


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


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)


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


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 main():
	(s, j, l) = (input(), -1, [-1])
	for c in s:
		while j != -1 and c != s[j]:
			j = l[j]
		j += 1
		l.append(j)
	le = l[-1]
	if l.count(le) < 2:
		le = l[le]
	print(s[:le] if le > 0 else 'Just a legend')
main()


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


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


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep=' ', end='\n'):
	print(*var, file=sys.stderr, end=end, sep=sep)
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
s = input()
n = len(s)
ans = 'Just a legend'
p = [0] * n
l = 0
i = 1
d = dd(int)
count = 0
while i < n:
	if s[i] == s[l]:
		l += 1
		p[i] = l
		d[p[i]] += 1
	elif l != 0:
		l = p[l - 1]
		i -= 1
	i += 1
if p[-1] != 0 and d[p[-1]] >= 2:
	ans = s[:p[-1]]
elif p[-1] != 0 and p[p[-1] - 1] > 0:
	ans = s[:p[p[-1] - 1]]
print(ans)


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


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


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


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


def kmp_fail(p):
	m = len(p)
	fail = m * [0]
	k = 0
	j = 1
	while j < m:
		if p[j] == p[k]:
			fail[j] = k + 1
			j += 1
			k += 1
		elif k > 0:
			k = fail[k - 1]
		else:
			j += 1
	return fail
s = input()
f = kmp_fail(s)
ans = f[-1]
if f.count(f[-1]) > 1 and ans != 0:
	print(s[0:ans])
elif f[ans - 1] > 0:
	print(s[0:f[ans - 1]])
else:
	print('Just a legend')


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']
a = lsi()
l = len(a)
kmp = [-1] * l
j = -1
for i in range(1, l):
	while j >= 0 and a[i] != a[j + 1]:
		j = kmp[j]
	if a[i] == a[j + 1]:
		j += 1
	kmp[i] = j
m = kmp[-1]
if kmp.count(m) == 1:
	m = kmp[m]
if m < 0:
	print('Just a legend')
else:
	print(*a[:m + 1], sep='')


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


def KMPPreprocess(p):
	m = len(p)
	kmp = [0] * m
	i = 1
	j = 0
	while i < m:
		if p[i] == p[j]:
			j += 1
			kmp[i] = j
			i += 1
		elif j == 0:
			kmp[i] = j
			i += 1
		else:
			j = kmp[j - 1]
	return kmp
s = input()
kmp = KMPPreprocess(s)
res = 0
for i in range(1, len(s) - 1):
	if kmp[i] != 0:
		if kmp[i] == kmp[-1]:
			res = kmp[i]
			break
if res == 0 and kmp[-1] != 0 and (kmp[kmp[-1] - 1] != 0):
	res = kmp[kmp[-1] - 1]
print(s[:res] if res > 0 else 'Just a legend')


s = input()

def kmp_fail(p):
	m = len(p)
	fail = m * [0]
	k = 0
	j = 1
	while j < m:
		if p[j] == p[k]:
			fail[j] = k + 1
			j += 1
			k += 1
		elif k > 0:
			k = fail[k - 1]
		else:
			j += 1
	return fail
l = [-1]
l += kmp_fail(s)
le = l[-1]
if l.count(le) < 2:
	le = l[le]
print(s[:le] if le > 0 else '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')


from sys import stdin, stdout

def min(x, y):
	if x < y:
		return x
	else:
		return y

def max(x, y):
	if x > y:
		return x
	else:
		return y

def binarysearch(a, x, first, last):
	if a[last] <= x:
		return a[last]
	if a[first] > x:
		return 0
	mid = (first + last) // 2
	if a[mid] == x:
		return x
	if a[mid] < x and a[mid + 1] > x:
		return a[mid]
	if a[mid] < x:
		return binarysearch(a, x, mid, last)
	return binarysearch(a, x, first, mid)
s = stdin.readline().strip()
if len(s) == 1:
	print('Just a legend')
	exit()
z = []
for i in range(len(s)):
	z.append(0)
x = 0
y = 0
n = len(s)
for i in range(1, n):
	z[i] = max(0, min(z[i - x], y - i + 1))
	while i + z[i] < n and s[z[i]] == s[i + z[i]]:
		x = i
		y = i + z[i]
		z[i] += 1
a = [0]
for i in range(len(s) - 1, -1, -1):
	if z[i] == len(s) - i:
		a.append(len(s) - i)
ans = 0
mx = 0
v = []
for i in range(1, n):
	if z[i] + i == n:
		v.append(z[i])
		mx = max(mx, z[i] - 1)
	else:
		mx = max(mx, z[i])
for i in range(0, len(v)):
	t = v[i]
	if t <= mx:
		ans = max(ans, t)
if ans == 0:
	print('Just a legend')
else:
	for i in range(0, ans):
		print(s[i], end='')


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


def preZ(s):
	n = len(s)
	z = [0] * n
	z[0] = n
	r = 0
	if n == 1:
		return z
	while r + 1 < n and s[r] == s[r + 1]:
		r += 1
	z[1] = r
	l = 1 if r > 0 else 0
	for k in range(2, n):
		bl = r + 1 - k
		gl = z[k - l]
		if gl < bl:
			z[k] = z[k - l]
		else:
			j = max(0, r - k + 1)
			while k + j < n and s[j] == s[k + j]:
				j += 1
			z[k] = j
			(l, r) = (k, k + j - 1)
	return z

def f(s):
	n = len(s)
	if n == 1:
		return 'Just a legend'
	z = preZ(s)
	maxm = max(preZ(s[:-1])[1:], default=0)
	ans = max([z[i] for i in range(n) if z[i] <= maxm and i + z[i] == n], default=0)
	if ans == 0:
		return 'Just a legend'
	return s[:ans]
print(f(input()))


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 prefix(s):
	p = [0] * len(s)
	for i in range(1, len(s)):
		k = p[i - 1]
		while k > 0 and s[k] != s[i]:
			k = p[k - 1]
		if s[k] == s[i]:
			k += 1
		p[i] = k
	return p
string = input()
prefix_values = prefix(string)
n = len(string)
if prefix_values[n - 1] == 0:
	print('Just a legend')
	exit()
last_pr = prefix_values[n - 1]
for i in range(n - 1):
	if prefix_values[i] == last_pr:
		print(string[:prefix_values[i]])
		exit()
next_pos_value = prefix_values[last_pr - 1]
if next_pos_value == 0:
	print('Just a legend')
	exit()
for i in range(n):
	a = prefix_values[i]
	if prefix_values[i] == next_pos_value and i != last_pr:
		print(string[:prefix_values[i]])
		exit()


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


maxZ = pos = 0

def ZFunction(S):
	L = R = 0
	N = len(S)
	Z = [0] * N
	Z[0] = N
	for i in range(1, N):
		if i > R:
			L = R = i
			while R < N and S[R] == S[R - L]:
				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] == S[R - L]:
					R += 1
				Z[i] = R - L
				R -= 1
	return Z
S = input()
N = len(S)
Z = ZFunction(S)
for i in range(1, N):
	if Z[i] == N - i and maxZ >= N - i:
		pos = N - i
		break
	maxZ = max(maxZ, Z[i])
if pos == 0:
	print('Just a legend')
else:
	print(S[:pos])


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

def get_longest_substr(string):
	long_pref_suff = get_lps(string)
	n = len(string)
	if long_pref_suff[n - 1] == 0:
		return -1
	for i in range(0, n - 1):
		if long_pref_suff[i] == long_pref_suff[n - 1]:
			return string[0:long_pref_suff[i]]
	if long_pref_suff[long_pref_suff[n - 1] - 1] == 0:
		return -1
	else:
		return string[0:long_pref_suff[long_pref_suff[n - 1] - 1]]

def main(string):
	res = get_longest_substr(string)
	if res == -1:
		print('Just a legend')
	else:
		print(res)
main(input())


def TheZFunction(s):
	z = [0] * len(s)
	z[0] = -1
	l = r = -1
	for k in range(1, len(s)):
		if s[k] != s[0]:
			z[k] = 0
		elif l <= k <= r:
			if k + z[k - l] < r:
				z[k] = z[k - l]
				continue
			if k + z[k - l] >= r:
				z[k] = r - k
				i = z[k]
				j = r
				while j < len(s) and s[i] == s[j]:
					i += 1
					j += 1
					z[k] += 1
				l = k
				r = j - 1
		else:
			i = 0
			j = k
			while j < len(s) and s[i] == s[j]:
				i += 1
				j += 1
				z[k] += 1
			l = k
			r = j - 1
	return z
s = input()
z = TheZFunction(s)
maxZ = 0
res = 0
n = len(s)
for i in range(1, n):
	if z[i] == n - i and maxZ >= n - i:
		res = n - i
		break
	else:
		maxZ = max(z[i], maxZ)
if res:
	print(s[:res])
else:
	print('Just a legend')


def prefix_function(s):
	pi = [0]
	n = len(s)
	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.append(j)
	return pi
s = input()
n = len(s)
pi = prefix_function(s)
possible_middles = set(pi[0:-1])
j = n - 1
while pi[j] not in possible_middles and j > 0:
	j = pi[j - 1]
if pi[j] == 0:
	print('Just a legend')
else:
	print(s[0:pi[j]])


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


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


def KMPpreprocess(p, prefix):
	prefix[0] = 0
	m = len(p)
	j = 0
	i = 1
	while i < m:
		if p[i] == p[j]:
			j += 1
			prefix[i] = j
			i += 1
		elif j != 0:
			j = prefix[j - 1]
		else:
			prefix[i] = 0
			i += 1

def KMPCheck(len, prefix):
	curLeng = prefix[len - 1]
	res = 0
	while curLeng > 0:
		for i in range(len - 2, 0, -1):
			if prefix[i] >= curLeng:
				res = max(res, curLeng)
				break
		if res > 0:
			return res
		else:
			curLeng = prefix[curLeng - 1]
	return 0
t = input()
n = len(t)
prefix = [0] * n
KMPpreprocess(t, prefix)
x = KMPCheck(n, prefix)
if x == 0:
	print('Just a legend')
else:
	print(t[:x])