#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
	const int h, *g;
	SAComp(int h, int* g) : h(h), g(g) {}
	bool operator() (int a, int b) {
		return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
	}
};
int *buildSA(char* t, int n) {
	int g[n+1], b[n+1], *v = new int[n+1];
	REP(i,n+1) v[i] = i, g[i] = t[i];
	b[0] = 0; b[n] = 0;

	sort(v, v+n+1, SAComp(0, g));
	for(int h = 1; b[n] != n ; h *= 2) {
		SAComp comp(h, g);
		sort(v, v+n+1, comp);
		REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
		REP(i, n+1) g[v[i]] = b[i];
	}
	return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
	int a = 0, b = n;
	while (a < b) {
		int c = (a + b) / 2;
		if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
	}
	return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
	int h = 0, b[n+1], *lcp = new int[n+1];
	REP(i, n+1) b[a[i]] = i;
	REP(i, n+1) {
		if (b[i]){
			for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
			lcp[b[i]] = h;
		} else lcp[b[i]] = -1;
		if (h > 0) --h;
	}
	return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
	int logn = 1;
	for (int k = 1; k < n; k *= 2) ++logn;
	int *r = new int[n * logn];
	int *b = r; copy(a, a+n, b);
	for (int k = 1; k < n; k *= 2) {
		copy(b, b+n, b+n); b += n;
		REP(i, n-k) b[i] = min(b[i], b[i+k]);
	}
	return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
	int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
	s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
	return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
	int i = o;
	for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
	return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
static int tres;
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
	int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
	if (!COMP(rh, sa[r])) return -1;
	for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
		int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
		if (A >= B) {
			if (lh < A) l = k;
			else if (lh > A) r = k, rh = A;
			else {
				int i = computeLCP(t, n+1, p, m, A, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		} else {
			if (rh < B) r = k;
			else if (rh > B) l = k, lh = B;
			else {
				int i = computeLCP(t, n+1, p, m, B, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		}
	}
	tres=r+1;
	return rh == m ? sa[r] : -1;
}
char input[1000001];
char ans  [1000001];
int anslen=0;
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=0;i--){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		if(input[0]!=input[n-i-1])continue;

		ans[i+1]=0;
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,SA,RMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
	const int h, *g;
	SAComp(int h, int* g) : h(h), g(g) {}
	bool operator() (int a, int b) {
		return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
	}
};
int *buildSA(char* t, int n) {
	int g[n+1], b[n+1], *v = new int[n+1];
	REP(i,n+1) v[i] = i, g[i] = t[i];
	b[0] = 0; b[n] = 0;

	sort(v, v+n+1, SAComp(0, g));
	for(int h = 1; b[n] != n ; h *= 2) {
		SAComp comp(h, g);
		sort(v, v+n+1, comp);
		REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
		REP(i, n+1) g[v[i]] = b[i];
	}
	return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
	int a = 0, b = n;
	while (a < b) {
		int c = (a + b) / 2;
		if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
	}
	return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
	int h = 0, b[n+1], *lcp = new int[n+1];
	REP(i, n+1) b[a[i]] = i;
	REP(i, n+1) {
		if (b[i]){
			for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
			lcp[b[i]] = h;
		} else lcp[b[i]] = -1;
		if (h > 0) --h;
	}
	return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
	int logn = 1;
	for (int k = 1; k < n; k *= 2) ++logn;
	int *r = new int[n * logn];
	int *b = r; copy(a, a+n, b);
	for (int k = 1; k < n; k *= 2) {
		copy(b, b+n, b+n); b += n;
		REP(i, n-k) b[i] = min(b[i], b[i+k]);
	}
	return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
	int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
	s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
	return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
	int i = o;
	for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
	return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
static int tres;
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
	int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
	if (!COMP(rh, sa[r])) return -1;
	for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
		int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
		if (A >= B) {
			if (lh < A) l = k;
			else if (lh > A) r = k, rh = A;
			else {
				int i = computeLCP(t, n+1, p, m, A, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		} else {
			if (rh < B) r = k;
			else if (rh > B) l = k, lh = B;
			else {
				int i = computeLCP(t, n+1, p, m, B, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		}
	}
	tres=r+1;
	return rh == m ? sa[r] : -1;
}
char input[1111111];
char ans  [1111111];
int anslen=0;
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=0;i--){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		if(input[0]!=input[n-i-1])continue;

		ans[i+1]=0;
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,SA,RMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
int n;
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
	const int h, *g;
	SAComp(int h, int* g) : h(h), g(g) {}
	bool operator() (int a, int b) {
		return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
	}
};
int *buildSA(char* t, int n) {
	int g[n+1], b[n+1], *v = new int[n+1];
	REP(i,n+1) v[i] = i, g[i] = t[i];
	b[0] = 0; b[n] = 0;

	sort(v, v+n+1, SAComp(0, g));
	for(int h = 1; b[n] != n ; h *= 2) {
		SAComp comp(h, g);
		sort(v, v+n+1, comp);
		REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
		REP(i, n+1) g[v[i]] = b[i];
	}
	return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
	int a = 0, b = n;
	while (a < b) {
		int c = (a + b) / 2;
		if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
	}
	if(a==n)return 0;
	return strncmp(t+sa[a+1], p, m) == 0 ? 1 : 0;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
	int h = 0, b[n+1], *lcp = new int[n+1];
	REP(i, n+1) b[a[i]] = i;
	REP(i, n+1) {
		if (b[i]){
			for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
			lcp[b[i]] = h;
		} else lcp[b[i]] = -1;
		if (h > 0) --h;
	}
	return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
	int logn = 1;
	for (int k = 1; k < n; k *= 2) ++logn;
	int *r = new int[n * logn];
	int *b = r; copy(a, a+n, b);
	for (int k = 1; k < n; k *= 2) {
		copy(b, b+n, b+n); b += n;
		REP(i, n-k) b[i] = min(b[i], b[i+k]);
	}
	return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
	int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
	s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
	return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
	int i = o;
	for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
	return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
	int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
	if (!COMP(rh, sa[r])) return -1;
	for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
		int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
		if (A >= B) {
			if (lh < A) l = k;
			else if (lh > A) r = k, rh = A;
			else {
				int i = computeLCP(t, n+1, p, m, A, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		} else {
			if (rh < B) r = k;
			else if (rh > B) l = k, lh = B;
			else {
				int i = computeLCP(t, n+1, p, m, B, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		}
	}
	return rh == m ? sa[r] : -1;
}
char input[1111111];
char ans  [1111111];
int anslen=0;
int main() {
	gets(input);
	n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	/*
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	*/
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	//int *sufLCP=buildLCP(input+1,n-1,sufSA);
	//int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=0;i--){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		if(input[0]!=input[n-i-1])continue;

		ans[i+1]=0;
		if(find(input+1,n-1,ans,i+1,sufSA)){
			anslen=i+1;
			break;
		}
		/*
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,sufSA,sufRMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		*/
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
  const int h, *g;
  SAComp(int h, int* g) : h(h), g(g) {}
  bool operator() (int a, int b) {
    return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
  }
};
int *buildSA(char* t, int n) {
  int g[n+1], b[n+1], *v = new int[n+1];
  REP(i,n+1) v[i] = i, g[i] = t[i];
  b[0] = 0; b[n] = 0;

  sort(v, v+n+1, SAComp(0, g));
  for(int h = 1; b[n] != n ; h *= 2) {
    SAComp comp(h, g);
    sort(v, v+n+1, comp);
    REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
    REP(i, n+1) g[v[i]] = b[i];
  }
  return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
  int a = 0, b = n;
  while (a < b) {
    int c = (a + b) / 2;
    if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
  }
  return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
  int h = 0, b[n+1], *lcp = new int[n+1];
  REP(i, n+1) b[a[i]] = i;
  REP(i, n+1) {
    if (b[i]){
      for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
      lcp[b[i]] = h;
    } else lcp[b[i]] = -1;
    if (h > 0) --h;
  }
  return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
  int logn = 1;
  for (int k = 1; k < n; k *= 2) ++logn;
  int *r = new int[n * logn];
  int *b = r; copy(a, a+n, b);
  for (int k = 1; k < n; k *= 2) {
    copy(b, b+n, b+n); b += n;
    REP(i, n-k) b[i] = min(b[i], b[i+k]);
  }
  return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
  int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
  s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
  s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
  return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
  int i = o;
  for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
  return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
  int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
  if (!COMP(rh, sa[r])) return -1;
  for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
    int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
    if (A >= B) {
      if (lh < A) l = k;
      else if (lh > A) r = k, rh = A;
      else {
        int i = computeLCP(t, n+1, p, m, A, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    } else {
      if (rh < B) r = k;
      else if (rh > B) l = k, lh = B;
      else {
        int i = computeLCP(t, n+1, p, m, B, sa[k]);
        if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
      }
    }
  }
  return rh == m ? sa[r] : -1;
}
typedef long long lli;
lli mod_pow(lli a,lli b,lli M){
	if(b==0)return 1;
	lli res=mod_pow((a*a)%M,b/2,M);
	if(b&1)res=(res*a)%M;
	return res;
}
char input[1000001];
char ans  [1000001];
int anslen=0;

const lli MOD=1000000009;
int hash[1000001];
int main() {
	gets(input);
	int n=strlen(input);
	char last=input[n-1];
	input[n-1]=0;
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	for(int i=0;i<n;i++){
		ans[i]=input[i];
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,SA,RMQ)!=-1){
				anslen=i+1;
			}
			input[n-1]=last;
		}
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;

typedef long long int lli;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=(x);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)

long long mod_pow(long long a,long long n,long long M){
	if(n==0)return 1;
	long long res=mod_pow(a*a%M,n/2,M);
	if(n&1)res=res*a%M;
	return res;
}
//任意の区間のハッシュ文字列をO(1)で求める
//前処理はO(L)
const int P1=293,P2=1000000009;//P2は素数,P1も素数が望ましい
const int MAX_L=1000001;
int pow_P1[MAX_L],rev_P1[MAX_L];
int str_hash[MAX_L];
void init_hash(const string &in){
	*pow_P1=1;*rev_P1=1;
	int rev=mod_pow(P1,P2-2,P2);;
	for(int i=0;i<MAX_L-1;i++){
		pow_P1[i+1]=((lli)pow_P1[i]*P1)%P2;
	}
	for(int i=0;i<MAX_L-1;i++){
		rev_P1[i+1]=((lli)rev_P1[i]*rev)%P2;
	}
	*str_hash=0;
	REP(i,in.size()){
		str_hash[i+1]=(str_hash[i]+(lli)pow_P1[i]*in[i])%P2;
	}
}
//文字列の[s,e)に関するハッシュ値を求める
int calc_hash(int s,int e){
	return ((lli)(((lli)str_hash[e]-str_hash[s]+P2)%P2)*rev_P1[s])%P2;
}

//ローリングハッシュ
template <int B,int M>
class RollingHash{
private:
	int value;
	int p;//ハッシュの長さ
	int hash_back;
public:
	int operator()() const{
		return value;
	}
	void set_length(int p_){
		hash_back=mod_pow(B,p_-1,M);p=p_;
	}
	explicit RollingHash(int p=0):p(p){value=0;set_length(p);}
	void add(int v){//後ろにvを追加する
		value=((long long)value*B+v)%M;
	}
	void remove(int v){
		value=(((long long)value+M-(long long)hash_back*v%M))%M;
	}
	bool operator==(const RollingHash &a){
		return a.value==value;
	}
};

bool rabin_karp(const char *exp,const char *find){
	int m=strlen(find),i,n=strlen(exp);
	RollingHash<293,1000000009> find_hash(m),exp_hash(m);
	if(n<m)return false;
	for(i=0;i<m;i++){
		find_hash.add(find[i]);
		exp_hash.add(exp[i]);
	}
	i=0;
	while(1){
		if(find_hash()==exp_hash()){
			return true;
		}
		if(i>=n-m)break;
		exp_hash.remove(exp[i]);
		exp_hash.add((exp[i+m]));
		i++;
	}
	return false;
}
string in;
//二分探索
int max_length(string &in){
	char t=in[in.length()-1];
	in[in.length()-1]='!';
	int low=0,high=in.size();
	while(low+1<high){
		int mid=(low+high)/2;
		string pre=in.substr(0,mid);
		if(rabin_karp(in.c_str()+1,pre.c_str())){
			low=mid;
		}else{
			high=mid;
		}
	}
	in[in.length()-1]=t;
	return low;
}
int main(){
	cin>>in;
	int pre_len=max_length(in);
	init_hash(in);

	for(int i=pre_len;i>=1;i--){
		if(calc_hash(0,i)==calc_hash(in.size()-i,in.size())){
			cout<<in.substr(0,i)<<endl;
			return 0;
		}
	}
	cout<<"Just a legend"<<endl;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
	const int h, *g;
	SAComp(int h, int* g) : h(h), g(g) {}
	bool operator() (int a, int b) {
		return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
	}
};
int *buildSA(char* t, int n) {
	int g[n+1], b[n+1], *v = new int[n+1];
	REP(i,n+1) v[i] = i, g[i] = t[i];
	b[0] = 0; b[n] = 0;

	sort(v, v+n+1, SAComp(0, g));
	for(int h = 1; b[n] != n ; h *= 2) {
		SAComp comp(h, g);
		sort(v, v+n+1, comp);
		REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
		REP(i, n+1) g[v[i]] = b[i];
	}
	return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
	int a = 0, b = n;
	while (a < b) {
		int c = (a + b) / 2;
		if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
	}
	return strncmp(t+sa[a+1], p, m) == 0 ? 1 : 0;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
	int h = 0, b[n+1], *lcp = new int[n+1];
	REP(i, n+1) b[a[i]] = i;
	REP(i, n+1) {
		if (b[i]){
			for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
			lcp[b[i]] = h;
		} else lcp[b[i]] = -1;
		if (h > 0) --h;
	}
	return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
	int logn = 1;
	for (int k = 1; k < n; k *= 2) ++logn;
	int *r = new int[n * logn];
	int *b = r; copy(a, a+n, b);
	for (int k = 1; k < n; k *= 2) {
		copy(b, b+n, b+n); b += n;
		REP(i, n-k) b[i] = min(b[i], b[i+k]);
	}
	return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
	int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
	s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
	return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
	int i = o;
	for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
	return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
	int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
	if (!COMP(rh, sa[r])) return -1;
	for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
		int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
		if (A >= B) {
			if (lh < A) l = k;
			else if (lh > A) r = k, rh = A;
			else {
				int i = computeLCP(t, n+1, p, m, A, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		} else {
			if (rh < B) r = k;
			else if (rh > B) l = k, lh = B;
			else {
				int i = computeLCP(t, n+1, p, m, B, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		}
	}
	return rh == m ? sa[r] : -1;
}
char input[1111111];
char ans  [1111111];
int anslen=0;
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	/*
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	*/
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	//int *sufLCP=buildLCP(input+1,n-1,sufSA);
	//int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=0;i--){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		if(input[0]!=input[n-i-1])continue;

		ans[i+1]=0;
		if(find(input+1,n-1,ans,i+1,sufSA)){
			anslen=i+1;
			break;
		}
		/*
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,sufSA,sufRMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		*/
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define REP(i,x)for(int i=0;i<(int)x;i++)
// Larsson-Sadakane's Suffix array Construction: O(n (log n)^2)
struct SAComp {
	const int h, *g;
	SAComp(int h, int* g) : h(h), g(g) {}
	bool operator() (int a, int b) {
		return a == b ? false : g[a] != g[b] ? g[a] < g[b] : g[a+h] < g[b+h];
	}
};
int *buildSA(char* t, int n) {
	int g[n+1], b[n+1], *v = new int[n+1];
	REP(i,n+1) v[i] = i, g[i] = t[i];
	b[0] = 0; b[n] = 0;

	sort(v, v+n+1, SAComp(0, g));
	for(int h = 1; b[n] != n ; h *= 2) {
		SAComp comp(h, g);
		sort(v, v+n+1, comp);
		REP(i, n) b[i+1] = b[i] + comp(v[i], v[i+1]);
		REP(i, n+1) g[v[i]] = b[i];
	}
	return v;
}
// Naive matching O(m log n)
int find(char *t, int n, char *p, int m, int *sa) {
	int a = 0, b = n;
	while (a < b) {
		int c = (a + b) / 2;
		if (strncmp(t+sa[c], p, m) < 0) a = c+1; else b = c;
	}
	return strncmp(t+sa[a], p, m) == 0 ? sa[a] : -1;
}

// Kasai-Lee-Arimura-Arikawa-Park's simple LCP computation: O(n)
int *buildLCP(char *t, int n, int *a) {
	int h = 0, b[n+1], *lcp = new int[n+1];
	REP(i, n+1) b[a[i]] = i;
	REP(i, n+1) {
		if (b[i]){
			for (int j = a[b[i]-1]; j+h<n && i+h<n && t[j+h] == t[i+h]; ++h);
			lcp[b[i]] = h;
		} else lcp[b[i]] = -1;
		if (h > 0) --h;
	}
	return lcp;
}
// call RMQ = buildRMQ(lcp, n+1)
int *buildRMQ(int *a, int n) {
	int logn = 1;
	for (int k = 1; k < n; k *= 2) ++logn;
	int *r = new int[n * logn];
	int *b = r; copy(a, a+n, b);
	for (int k = 1; k < n; k *= 2) {
		copy(b, b+n, b+n); b += n;
		REP(i, n-k) b[i] = min(b[i], b[i+k]);
	}
	return r;
}
// inner LCP computation with RMQ: O(1)
int minimum(int x, int y, int *rmq, int n) {
	int z = y - x, k = 0, e = 1, s; // y - x >= e = 2^k なる最大 k
	s = ( (z & 0xffff0000) != 0 ) << 4; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000ff00) != 0 ) << 3; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x000000f0) != 0 ) << 2; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x0000000c) != 0 ) << 1; z >>= s; e <<= s; k |= s;
	s = ( (z & 0x00000002) != 0 ) << 0; z >>= s; e <<= s; k |= s;
	return min( rmq[x+n*k], rmq[y+n*k-e+1] );
}
// outer LCP computation: O(m - o)
int computeLCP(char *t, int n, char *p, int m, int o, int k) {
	int i = o;
	for (; i < m && k+i < n && p[i] == t[k+i]; ++i);
	return i;
}
// Mamber-Myers's O(m + log n) string matching with LCP/RMQ
#define COMP(h, k) (h == m || (k+h<n && p[h]<t[k+h]))
static int tres;
int find(char *t, int n, char *p, int m, int *sa, int *rmq) {
	int l = 0, lh = 0, r = n, rh = computeLCP(t,n+1,p,m,0,sa[n]);
	if (!COMP(rh, sa[r])) return -1;
	for (int k = (l+r)/2; l+1 < r; k = (l+r)/2) {
		int A = minimum(l+1, k, rmq, n+1), B = minimum(k+1, r, rmq, n+1);
		if (A >= B) {
			if (lh < A) l = k;
			else if (lh > A) r = k, rh = A;
			else {
				int i = computeLCP(t, n+1, p, m, A, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		} else {
			if (rh < B) r = k;
			else if (rh > B) l = k, lh = B;
			else {
				int i = computeLCP(t, n+1, p, m, B, sa[k]);
				if (COMP(i, sa[k])) r = k, rh = i; else l = k, lh = i;
			}
		}
	}
	tres=r+1;
	return rh == m ? sa[r] : -1;
}
char input[1111111];
char ans  [1111111];
int anslen=0;
int main() {
	gets(input);
	int n=strlen(input);
	if(n<=2){
		cout<<"Just a legend"<<endl;
		return 0;
	}
	char last=input[n-1];
	input[n-1]=0;
	/*
	int *SA=buildSA(input+1,n-2);
	int *LCP=buildLCP(input+1,n-2,SA);
	int *RMQ=buildRMQ(LCP, n-1);
	*/
	input[n-1]=last;
	int *sufSA=buildSA(input+1,n-1);
	int *sufLCP=buildLCP(input+1,n-1,sufSA);
	int *sufRMQ = buildRMQ(sufLCP, n);
	REP(i,n)ans[i]=input[i];
	for(int i=n-2;i>=0;i--){
		char t=ans[i+1];
		if(last!=ans[i])continue;
		if(input[0]!=input[n-i-1])continue;

		ans[i+1]=0;
		if(find(input+1,n-1,ans,i+1,sufSA,sufRMQ)+1==n-i-1){
			input[n-1]=0;
			if(find(input+1,n-2,ans,i+1,sufSA,sufRMQ)!=-1){
				anslen=i+1;
				break;
			}
			input[n-1]=last;
		}
		ans[i+1]=t;
	}
	if(anslen==0){
		puts("Just a legend");
	}else{
		ans[anslen]=0;
		puts(ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            int o=0;
            if(z[i-l]>=r-i+l)
            {
                for(;r<s.size()&&s[r]==s[r-l];r++,o++);
                z[i]=z[i-l]+o;
            }
            else
            z[i]=z[i-l];
        }
        z[i]=min(z[i],int(s.size())-i);
      //  cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=z[p])
            w=p;
        if(z[i]>=s.size()-i)
        p=i;

    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
  //  system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r)
            {
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-z[i-l];
            }
            else
            z[i]=z[i-l];
        }
        z[i]=min(z[i],int(s.size())-i);
      //  cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=z[p])
            w=p;
        if(z[i]>=s.size()-i)
        p=i;

    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
  //  system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r)
            {
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-z[i-l];
            }
            else
            z[i]=z[i-l];
        }
    //    cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=s.size()-i)
        p=i;
        else
        if(z[i]>=z[p])
            w=p;
    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
    system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r-i+l)
            {
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-z[i-l];
            }
            else
            z[i]=z[i-l];
        }
        z[i]=min(z[i],int(s.size())-i);
      //  cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=z[p])
            w=p;
        if(z[i]>=s.size()-i)
        p=i;

    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
  //  system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r)
            {
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-z[i-l];
            }
            else
            z[i]=z[i-l];
        }
        z[i]=min(z[i],int(s.size())-i);
      //  cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=z[p])
            w=p;
        if(z[i]>=s.size()-i)
        p=i;

    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
    system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r-i)
            {
                l=i;
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-l;
            }
            else
            z[i]=z[i-l];
        }
        z[i]=min(z[i],int(s.size())-i);
  //      cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=z[p])
            w=p;
        if(z[i]>=s.size()-i)
        p=i;

    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
   // system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r)
            {
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-z[i-l];
            }
            else
            z[i]=z[i-l];
        }
    //    cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=s.size()-i)
        p=i;
        else
        if(z[i]>=z[p])
            w=p;
    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
  //  system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r-i)
            {
                l=i;
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-l;
            }
            else
            z[i]=z[i-l];
        }
        z[i]=min(z[i],int(s.size())-i);
        cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=z[p])
            w=p;
        if(z[i]>=s.size()-i)
        p=i;

    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
   // system("pause");
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
string s;
int z[1000010],r,l;
int main()
{
    cin>>s;
    for(int i=1;i<s.size();i++)
    {
        if(r<=i)
        {
            l=r=i;
            for(;r<s.size()&&s[r]==s[r-l];r++);
            z[i]=r-l;
        }
        else
        {
            if(z[i-l]>=r-i+l)
            {
                l=i;
                for(;r<s.size()&&s[r]==s[r-l];r++);
                z[i]=r-l;
            }
            else
            z[i]=z[i-l];
        }
        z[i]=min(z[i],int(s.size())-i);
     //   cout<<z[i]<<" ";
    }
    int p=0;
    int w=0;
    for(int i=s.size()-1;i>=0;i--)
    {
        if(z[i]>=z[p])
            w=p;
        if(z[i]>=s.size()-i)
        p=i;

    }
    if(w==0)
    cout<<"Just a legend"<<endl;
    else
    {
        for(int i=w;i<s.size();i++)
        cout<<s[i];
        cout<<endl;
    }
    //system("pause");
}


#include<iostream>
#include<vector>
#include<string>
using namespace std;
typedef unsigned long long ull;
const ull B=100000007;

vector<string> mem;

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		int i;
		for(i=1;i<=S.length()/3+1;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem.push_back(a);
		}

		for(i=mem.size()-1;i>=0;--i)
		{
			a=mem[i];
			b=S.substr(a.length(),S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			cout<<"Just a legend"<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
const int length=1000000;
int main()
{
	char *s;
	char *t1;
	char *t2;
	char *str;
	char *nstr;
	s=new char[length];
	t1=new char[length/3];
	t2=new char[length/3];
	str=new char[length/3];
	nstr=new char[length/3];
	while(cin>>s)
	{
		int save;
		int nsave=0;
		int flag=0;
		memset(t1,0,sizeof(t1));
		memset(t2,0,sizeof(t2));
		int len=strlen(s);
		int count1=0;
		char *tmp;
		for(int i=0;i<len;i++)
		{
			flag=0;
			int del=0;
			t1[count1]=s[i];
			t2[count1]=s[len-1-i];
			count1++;
			t1[count1]=0;
			t2[count1]=0;
			strrev(t2);
			if(strcmp(t1,t2)==0)
			{
				for(int j=1;j<len-1;j++)
				{
					int count=0;
					if(s[j]==t1[0])
					{
						int h;
						for(h=j;h<j+count1 && h!=len-1;h++,count++)
							if(s[h]!=t1[count])
							{
								break;
							}
						if(count==count1)
						{
							flag=1;
							break;
						}
					}
				}
			}
			strrev(t2);
			if(flag)
			{
				strcpy(str,t1);
				save=count1;
				if(save>nsave)
				{
					nsave=save;
					strcpy(nstr,str);
				}
			}
		}
		if(nsave>0)
		{
			nstr[count1]=0;
			cout<<nstr<<endl;
		}
		else
			cout<<"Just a legend"<<endl;
	}
	delete []s;
	delete []t1;
	delete []t2;
	delete []str;
	delete []nstr;
}


#include<cstdio>
#define max(x,y) ((x)>(y)?(x):(y))
const int MAXN=111111;

int a[MAXN];

int main()
{
	int n,t;
	scanf("%d%d",&n,&t);

	int i;

	for(i=0;i<n;++i)
		scanf("%d",&a[i]);

	int ans=0,tans=0;
	int sum=0;
	int pos=0,next=0;
	while(true)
	{
		while(next<n&&sum<=t)
		{
			++tans;
			sum+=a[next++];
		}
		--next;
		--tans;
		sum-=a[pos++];

		ans=max(ans,tans);

		if(pos>=n-1) break;
	}
	printf("%d\n",ans);

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned long long ull;
const ull B=100000007;

int all;
string mem[5555555];

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		int i;
		for(i=1,all=0;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem[all++]=a;
		}

		for(i=all-1;i>=0;++i)
		{
			a=mem[i];
			b=S.substr(a.length()-1,S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			printf("Just a legend\n");
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned long long ull;
const ull B=100000007;

int all;
string mem[555555];

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		int i;
		for(i=1,all=0;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem[all++]=a;
		}

		for(i=all-1;i>=0;++i)
		{
			a=mem[i];
			b=S.substr(a.length()-1,S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			printf("Just a legend\n");
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char ch[1000005];
int next[1000005];
int len=0;
void getnext()
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1 || ch[i]==ch[j])
        {
            i++;
            j++;
            next[i]=j;
        }
        else
        j=next[j];
    }
}
char goal[1000005];
int main()
{
    bool flag=0;
    int i,j,k;
    scanf("%s",&ch);
    len=strlen(ch);
    getnext();
    if(next[len-1]==0)
    printf("Just a legend\n");
    else
    {
        for(i=0;i<next[len];i++)
        {
            goal[i]=ch[i];
        }
        for(i=1;i<len-next[len];i++)
        {
            if(ch[i]==goal[0])
            {
                for(j=i;j<i+next[len];j++)
                {
                    if(ch[j]!=goal[j-i])
                    break;
                }
                if(j==i+next[len])
                flag=1;
            }
        }
        if(flag)
        printf("%s\n",goal);
        else
        printf("Just a legend\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
const int length=1000000;
int main()
{
	char *s;
	char *t1;
	char *t2;
	char *str;
	char *nstr;
	s=new char[length];
	t1=new char[length/3];
	t2=new char[length/3];
	str=new char[length/3];
	nstr=new char[length/3];
	while(cin>>s)
	{
		int save;
		int nsave=0;
		int flag=0;
		int len=strlen(s);
		int count1=0;
		char *tmp;
		for(int i=0;i<len;i++)
		{
			flag=0;
			int del=0;
			t1[count1]=s[i];
			t2[count1]=s[len-1-i];
			count1++;
			t1[count1]=0;
			t2[count1]=0;
			strrev(t2);
			if(strcmp(t1,t2)==0)
			{
				for(int j=1;j<len-1;j++)
				{
					int count=0;
					if(s[j]==t1[0])
					{
						if(strncmp(t1,s+j,count1)==0 && j+count1<len)
						{
							flag=1;
							break;
						}
					}
				}
			}
			strrev(t2);
			if(flag)
			{
				strcpy(str,t1);
				save=count1;
				if(save>nsave)
				{
					nsave=save;
					strcpy(nstr,str);
				}
			}
		}
		if(nsave>0)
		{
			nstr[count1]=0;
			cout<<nstr<<endl;
		}
		else
			cout<<"Just a legend"<<endl;
	}
	delete []s;
	delete []t1;
	delete []t2;
	delete []str;
	delete []nstr;
}


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char ch[1000005];
int next[1000005];
char goal[1000005];
int len=0;
void getnext()
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1 || ch[i]==ch[j])
        {
            i++;
            j++;
            next[i]=j;
        }
        else
        j=next[j];
    }
}
void getnextgoal()
{
	int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1 || goal[i]==goal[j])
        {
            i++;
            j++;
            next[i]=j;
        }
        else
        j=next[j];
    }
}
int main()
{
    bool flag=0;
    int i,j,k;
    scanf("%s",&ch);
    len=strlen(ch);
    getnext();
    if(next[len]==0)
    printf("Just a legend\n");
    else
    {
        for(i=0;i<next[len];i++)
        {
            goal[i]=ch[i];
        }
		goal[next[len]]='\0';
        for(i=1;i<len-next[len];i++)
        {
            if(ch[i]==goal[0])
            {
                for(j=i;j<i+next[len];j++)
                {
                    if(ch[j]!=goal[j-i])
                    break;
                }
                if(j==i+next[len])
				{
					flag=1;
					break;
				}
            }
        }
        if(flag)
        printf("%s\n",goal);
        else
		{
			int flag2=1;
			int len2=next[len];
			int len3=strlen(goal);
			getnextgoal();
			if(next[len3]==0)
                printf("Just a legend\n");
			else
			{
				for(i=0;i<next[len3];i++)
				{
					if(goal[i]!=ch[len-next[len3]+i])
					{
						flag2=0;
						break;
					}
				}
				if(flag2)
					for(i=0;i<next[len3];i++)
						printf("%c",goal[i]);
				else
					printf("Just a legend");
				printf("\n");
			}
		}
    }
    return 0;
}


//============================================================================
// Name        : xiaosai.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <queue>
using namespace std;

int fail[1000010];
bool vis[1000010];
char s[1000010];

int main () {
	int i, k;
	scanf("%s", s);
	memset(fail, 0xff, sizeof(fail));
	memset(vis, 0, sizeof(vis));
	for (i = 1; s[i]; i++) {
		for (k = fail[i-1]; k >= 0 && s[i] != s[k+1]; k = fail[k]);
		if (s[k+1] == s[i])	fail[i] = k+1;
		if (s[i+1] && fail[i] != -1)	vis[fail[i]] = 1;
	}
	i = fail[(int)strlen(s)-1];
	while (i != -1 && !vis[i])	i = fail[i];
	if (i == -1)
		printf("Just a legend\n");
	else {
		s[i+1] = '\0';
		printf("%s\n", s);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned __int64 ull;
const ull B=100000007;

int all;
string mem[111111];

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		for(int i=1,all=0;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem[all++]=a;
		}

		for(i=all-1;i>=0;++i)
		{
			a=mem[i];
			b=S.substr(a.length(),S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			printf("Just a legend\n");
	}
	return 0;
}


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1000005
bool vis[maxn];
char s[maxn];
int next[maxn];
int main()
{
	int i;
	scanf("%s",s);
	memset(vis,false,sizeof(vis));
	int len=strlen(s);
	next[0]=-1;
	next[1]=0;
	int k=0;
	for(i=2;i<=len;i++)
	{
		while(k>=0&&s[i-1]!=s[k])
		{
			k=next[k];
		}
		next[i]=++k;
	}//后缀数组的建立
    // for(int ii=1;ii<=len;ii++)printf("%d ",next[ii] );cout<<endl;
	if(next[len]<=0)
	{
		printf("Just a legend");
	}//当最后一位为0时说明没有与前缀相同的后缀存在
	else
	{
		vis[next[len]]=true;
		k=next[len];
		while(k>0)
		{
			vis[next[k]]=true;
			k=next[k];
		}//通过后缀找出所有的可行的长度
		int ans=0;
		bool ok=false;
		for(i=2;i<len;i++)//从i=2开始保证不以首字母开头
		{
			if(vis[next[i]]&&next[i]>ans)//当该长度可行且长度大于0则有解
			{
				ans=next[i];
				ok=true;
			}
		}
		if(!ok)
			printf("Just a legend");
		else
		for(i=0;i<ans;i++)
			printf("%c",s[i]);
	}
	cout<<endl;
	return 0;
}

/*
题意：找出与字符串前缀和后缀都相同的最长的子序列，且该子序列存在于字符串的额中部
解题思路：利用KMP算法找出整个字符串的后缀数组，然后利用后缀数组来找出所有的可能长度，然后从字符串第二位开始，若能找到可行的后缀长度，则该解可行，找出最大的长度即可
*/


#include <iostream>
#include <cstdio>
#include <cstring>
#define LMT 1000005
/****
KMP算法本质：在字符串中有一个字符，以这个字符为末尾的长度为N的子串(不包括末字符,N为所能取到的最大值)，
可以与字符串头字符构成的长度为N的字串匹配(不包括末字符，且保证末字符不等)。
***/
//第一次的next是最大的，之后就逐个减小
using namespace std;
int hash[LMT],next[LMT],len;
char str[LMT];
void init(void)
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1||str[i]==str[j])
        {
            i++;j++;next[i]=j;
        }
        else j=next[j];
    }
    for(i=0;i<len;i++)
    hash[next[i]]++;
}
int main()
{
    int i;
    scanf("%s",str);
    len=strlen(str);
    init();
    i=len;
    while(next[i]>0)
    {
       if(hash[next[i]])
       {
        for(int j=0;j<next[i];j++)
        printf("%c",str[j]);
        printf("\n");
        return 0;
       }
       i=next[i];
    }
    printf("Just a legend\n");
    return 0;
}


#include<iostream>
#include<vector>
#include<string>
using namespace std;
typedef unsigned __int64 ull;
const ull B=100000007;

vector<string> mem;

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		int i;
		for(i=1;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem.push_back(a);
		}

		for(i=mem.size()-1;i>=0;--i)
		{
			a=mem[i];
			b=S.substr(a.length(),S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			cout<<"Just a legend"<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned long long ull;
const ull B=100000007;

vector<string> mem;

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		int i;
		for(i=1;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem.push_back(a);
		}

		for(i=mem.size()-1;i>=0;--i)
		{
			a=mem[i];
			b=S.substr(a.length(),S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			printf("Just a legend\n");
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
char ch[1000005];
int main()
{
 //   freopen("in.txt","r",stdin);
    while(cin>>ch)
    {
        int len=strlen(ch),item=0;
        for(int i=1;i<len;i++)
        {
            if(ch[i]==ch[0])
            {
                int j=0;
                while(ch[j]==ch[i+j])
                    j++;
                if(i+j<len&&j>item)
                    item=j;
            }
        }
        if(item==0)
            cout<<"Just a legend"<<endl;
        else
        {
            ch[item]='\0';
            cout<<ch<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned long long ull;
const ull B=100000007;

vector<string> mem;

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		int i;
		for(i=1;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem.push_back(a);
		}

		for(i=mem.size()-1;i>=0;--i)
		{
			a=mem[i];
			b=S.substr(a.length()-1,S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			printf("Just a legend\n");
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned long long ull;
const ull B=100000007;

int all;
string mem[111111];

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		int i;
		for(i=1,all=0;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem[all++]=a;
		}

		for(i=all-1;i>=0;++i)
		{
			a=mem[i];
			b=S.substr(a.length(),S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			printf("Just a legend\n");
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned long long ull;
const ull B=100000007;

int all;
string mem[111111];

bool contain(string a,string b)
{
	int al=a.length(),bl=b.length();

	if(al>bl)
		return false;

	int i;

	ull t=1;

	for(i=0;i<al;++i) t*=B;

	ull ah=0,bh=0;

	for(i=0;i<al;++i) ah=ah*B+a[i];
	for(i=0;i<al;++i) bh=bh*B+b[i];

	for(i=0;i+al<=bl;++i)
	{
		if(ah==bh)
			return true;
		if(i+al<bl)
			bh=bh*B+b[i+al]-b[i]*t;
	}
	return false;
}

int main()
{
	string S;
	while(cin>>S)
	{
		string a;
		string b;
		for(int i=1,all=0;i<=S.length()/3;++i)
		{
			a=S.substr(0,i);
			b=S.substr(S.length()-i);

			if(a==b)
				mem[all++]=a;
		}

		for(i=all-1;i>=0;++i)
		{
			a=mem[i];
			b=S.substr(a.length(),S.length()-2*a.length());

			if(contain(a,b))
				break;
		}
		if(i>=0)
			cout<<a<<endl;
		else
			printf("Just a legend\n");
	}
	return 0;
}


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define ull unsigned long long
#define B 29
using namespace std;

char str[1001000];
int len,next[1001000],hash[1001000];
void GetNext() {
	int i = 0,j = -1;
	next[i] = -1;
	while (i < len) {
		if (j == -1 || str[i] == str[j])
			i++,j++,next[i] = j;
		else j = next[j];
	}
	for (i = 1; i <len; ++i){
        hash[next[i]]++;
	}
}
int main(){
   // freopen("in.cpp","r",stdin);
    while(~scanf("%s",str)){
        memset(hash,0,sizeof(hash));
        len=strlen(str);
        GetNext();
        int i=len;
        int f=0;
        while(next[i]!=0){
            if(hash[next[i]]){
                for(int j=0;j<next[i];++j)printf("%c",str[j]);
                printf("\n");
                f=1;
                break;
            }
            i=next[i];
        }
        if(!f){
            printf("Just a legend\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#define maxn 2000000
int next[maxn];
char arr[maxn];
int maxlen;
int max(int a,int b) {
	return a>b?a:b;
}
int min(int a,int b) {
	return a<b?a:b;
}
void get_next(char t[]) {
	int i = 1,j = 0;
	next[1] = 0;
	int len = strlen(t+1);
	while(i<=len) {
		if(j == 0 || t[i] == t[j]) {
			++i;++j;next[i] = j;
			if(i!=len)
				maxlen = max(maxlen,next[i]-1);
		}
		else j = next[j];
	}
	return;
}
int main() {
	int i,j;
	while(scanf("%s",arr+1)!=EOF) {
		int len = strlen(arr+1);
		arr[len+1] = '#';
		arr[len+2] = '\0';
		maxlen = 0;
		get_next(arr);
		int p = next[len+1];
		while(p-1>maxlen && p>=1) {
			p = next[p];
		}
		if(p<=1)printf("Just a legend\n");
		else {
			int ans = min(maxlen,p-1);
			for(i=1;i<=ans;i++)printf("%c",arr[i]);printf("\n");
		}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
char arr[1000006];
char ans[1000005];
struct nn{
	int pos;
	int num;
}a[1000005];
int main()
{
	int k,max,i,len1,len,j,h,mark,mark1,mm;
	char st;
	k=0;max=0;
	scanf("%s",arr);
	st=arr[0];ans[0]='\0';
	len1=strlen(arr);
	int sum=1;
	//for(i=1;i<len1;i++)
//	if(st==arr[i]){sum++;}
//	if(sum<3){printf("Just a legend\n");return 0;}
	for(i=1;i<len1;i++)
	if(st==arr[i]){a[k].pos=i;a[k++].num=1;}
	mark=0;
	for(h=0;h<k;h++)
	{
	mm=h;
			for(i=0,j=a[h].pos;j<len1;i++,j++)
			{
				if(arr[i]==arr[j]&&arr[i]==st&&i)mm++;
				if(arr[i]!=arr[j]){
					a[h].num=j-a[h].pos;
					if(max<a[h].num)max=a[h].num;
					break;
				}
			}
			if(j==len1)
			{
				a[h].num=j-a[h].pos;
				if(max>=a[h].num&&a[h].pos>1&&h){
				//	strcpy(ans,arr+a[h].num);

					mark=1;break;
				}
				 if(h==0)mm=h;
				 if(max<a[h].num)max=a[h].num;

			}h=mm;
		if(mark)break;
	}
	arr[a[h].num]='\0';
	if(mark)printf("%s\n",arr);
	else printf("Just a legend\n");
	return 0;
}


#include<cstdio>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>

char s[1000020];
int next[1000020];
int mark[1000020];
int main()
{
	while(scanf("%s",s+1)!=EOF)
	{
		int i=1,j=0;
		int len=strlen(s+1);
		memset(mark,0,sizeof(mark));
		s[len+1]='*';
		if(len<3){printf("Just a legend\n");continue;}
		next[1]=0;
		while(i<=len)
		{
			if(j==0||s[i]==s[j]){++i;++j;next[i]=j;}
			else j=next[j];
		}
		for(i=1;i<=len;i++)
		mark[next[i]]=1;
		int ans=0;
		int t=next[len+1];
		while(1)
		{
			if(mark[t])
			{
				ans=t;
				break;
			}
			t=next[t];
		}
		ans-=1;
		if(ans!=0)
		for(i=0;i<ans;i++)
		{
			printf("%c",s[i+1]);
		}
		else printf("Just a legend");
		printf("\n");
			//printf("%d ",next[i]);
		/*printf("\n");
		int last1,last2;
		last1=len;
		if(next[last1]==1)
		{
			if(s[1]!=s[len]){printf("Just a legend\n");continue;}
			int zzl;
			for(zzl=len-1;zzl>=2;zzl--)
				if(s[zzl]==s[1])break;
			if(zzl>=2)printf("%c\n",s[1]);
			else printf("Just a legend\n");
			continue;
		}
		while(next[last1]!=1)
			last1=next[last1];
		printf("%d\n",last1);
		for(last2=len-1;last2>=2;last2--)
		{
			int l=last2;
			while(next[l]!=1)
			{
				l=next[l];
			}
			if(l==last1)break;
		}
		printf("%d\n",last2);
		if(next[last2]!=1)
		{
			for(i=1;i<=next[last2];i++)
				printf("%c",s[i]);
			printf("\n");
		}
		else if(s[1]==s[len]&&s[1]==s[last2])
		{
			printf("%c\n",s[1]);
		}
		else printf("Just a legend\n");*/
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define M 104000
char next[M];

void getNext(char str[]) {
    int len = strlen(str);
    int i = 0, j = -1;
    next[0] = -1;
    while (i < len) {
        if (j == -1 || str[i] == str[j]) {
            i++;
            j++;
            next[i] = j;
        } else {
            j = next[j];
        }
    }
}
int res[M];

int KMP(char st1[], char st2[]) {
    // cout << st1 << endl << st2 << endl;
    int len1 = strlen(st1);
    int len2 = strlen(st2);
    int i, j;
    i = j = 0;
    int maxn = 0;
    while (i < len1 && j < len2) {
        if (j == -1 || st1[i] == st2[j]) {
            i++;
            j++;
            res[i] = j;
            //cout << i << " " << j << endl;
        } else {
            j = next[j];
        }
    }
    for (i = 2; i < len1; i++) {
        if (i + res[i] < len1 && maxn < res[i])
            maxn = res[i];
    }
    if (res[len1] > maxn)
        maxn = 0; //cout<<maxn<<endl;
    else maxn = res[len1];
    return maxn;
}
char str[M];
char st2[M];

int main() {
    int i, j;
    while (scanf("%s", str) != EOF) {
        int len = strlen(str);
        if (len <= 2) {
            puts("Just a legend");
            continue;
        }
        getNext(str);
        int ans = KMP(str + 1, str);
        if (ans <= 0) {
            puts("Just a legend");
            continue;
        }
        for (i = 0; i < ans; i++) {
            cout << str[i];
        }
        cout << endl;
    }
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char a[100006];
int len,next[100006],mak[100006];
int main()
{
	int i,j;
	cin>>a+1;
	memset(mak,0,sizeof(mak));
	len=strlen(a+1);
	next[1]=0;j=0;i=1;mak[0]=1;
	while(i<=len)
	{
		if(j==0||a[i]==a[j]){i++;j++;next[i]=j;if(i!=len)mak[j]=1;}
		else j=next[j];
	}
//	for(i=1;i<=len+1;i++)
	//cout<<next[i]<<"   ";
	int h=next[len+1];
	while(1)
	{
		if(mak[h])break;
		else h=next[h];
	}
//	cout<<h<<endl;
	if(h<=1)cout<<"Just a legend\n";
	else
	{
	    for(i=1;i<h;i++)
         	cout<<a[i];
	           cout<<endl;
	}

}


#include<stdio.h>
#include<string.h>
#define MAXN 1000005

int n;
char s[MAXN];
int next[MAXN];
bool have[MAXN];

void getnext(){
	int i=1,j=0;
	next[1]=0;
	while(i<n){
		if(j==0 || s[i]==s[j]){
			i++,j++;
			next[i]=j;
		}
		else j=next[j];
	}
}

void solve(){
	n=strlen(s+1);
	s[++n]='#';
	getnext();
	memset(have,false,sizeof(have));
	//for(int i=1;i<n;i++)printf("%d",next[i]);
//	printf("\n");
	for(int i=1;i<n;i++){
		have[next[i]]=true;
	}
	int res=next[n];

//	for(int i=0;i<n;i++)printf("%d",have[i]);
//	printf("\n");

	while(1){
		if(res<=1)break;
		if(have[res]){
		//	printf("ok");
			break;
		}
		else res=next[res];
	}

	if(res<=1)printf("Just a legend");
	else for(int i=1;i<res;i++)printf("%c",s[i]);
	printf("\n");

}

int main(){
	while(scanf("%s",s+1)!=EOF){
		solve();
	}
}


#include<stdio.h>
#include<string.h>
#define M 1000050
char St1[M];
char St2[M];
char St3[M];
char St4[M];
int next[M];
int num[M];
void GetNext(char st2[]){
	int len = strlen(st2);
	int i = 0,j = -1;
	next[0] = -1;
	while(i < len){
		if(j == -1||st2[i] == st2[j]){
			i++;
			j++;
			next[i] = j;
		}else{
			j = next[j];
		}
	}
}
int KMP(char st1[],char st2[]){
	int len1 = strlen(st1);
	int len2 = strlen(st2);
	int i = 0, j = 0,True=0;
	while(i < len1){
		if(j == -1||st1[i] == st2[j]){
			i++;
			j++;
		}else{
			j = next[j];
		}
		num[i]=j+1;
	}
	return True;
}
int main()
{
	int i;
	while (scanf ("%s",St2)){
		int j=0;
		int len2 = strlen(St2);
		for (i=1;i<strlen(St2)-1;i++){
			St1[j]=St2[i];
			j++;
		}
		St1[j]='\0';
		int len1 = strlen(St1);
		int max=-1,dian=0;
		for (i=0;i<len2;i++){
			next[i]=0;
			num[i]=0;
		}
		GetNext(St2);
		KMP(St1,St2);
		if (St1[0]==St2[0]){
			num[0]=1;
		}
		for (i=0;i<len1;i++){
			if (num[i]>max){
				dian=i;
				max=num[i];
			}
		}
		j=0;
		//for (i=dian-(max-1);i<=dian;i++){
		//	printf("%c",St1[i]);
		//}
		//printf("\n");
		for (i=dian-(max-1);i<=dian;i++){
			St4[j]=St1[i];
			j++;
		}
		St4[j]='\0';
	//	printf("%s\n",St4);
		j=0;
		for (i=len2-(max);i<len2;i++){
			St3[j]=St2[i];
			j++;
		}
		St3[j]='\0';
		//printf("%s\n",St3);
		int len3=strlen(St3);
		int len4=strlen(St4);
		GetNext(St4);
		KMP(St3,St4);
		if (num[len3-1]==0){
			printf("Just a legend\n");
		}
		else{
			for (i=len3-num[len3-1];i<len3;i++){
			printf("%c",St3[i]);
		}
		printf("\n");
		}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1000100
using namespace std;
int next[N],len;
char str[N];
void getnext(char s2[])
{
    memset(next,0,sizeof(next));
    int i=-1,j=0;
    next[0]=-1;
    while(j<strlen(s2))
    {
           if(i==-1||s2[i]==s2[j]){
               i++;  j++;
               next[j]=i;
           }
           else i=next[i];
    }
   // printf("%d\n",j);
}

int main()
{

	int i,j,ans;
	while(scanf("%s",str)!=EOF)
	{
		len=strlen(str);
		str[len++]='a';
		str[len]='\0';
		getnext(str);
		sort(next+1,next+len);
		ans=-10;
		for(i=len-1;i>=1;i--)
		{
			if(next[i]==next[i-1])
			{
			//	printf("%d %d\n",i,next[i]);
				ans=next[i];
				break;
			}
		}
		if(ans>0)
		{
			for(i=0;i<ans;i++)
			printf("%c",str[i]);
			printf("\n");
		}
		else printf("Just a legend\n");
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define M 1002
char st1[M];
char st2[M];
char st[1000002];
int next[M];

void GetNext(char st2[]){
	int len = strlen(st2);
	int i = 0,j = -1;
	next[0] = -1;
	while(i < len){
		if(j == -1||st2[i] == st2[j]){
			i++;
			j++;
			next[i] = j;
		}else{
			j = next[j];
		}
	}
}

int KMP(char st1[],char st2[],int i){
	int len1 = strlen(st1);
	int len2 = strlen(st2);
	int	j = 0;
	while(i < len1&&j < len2){
		if(j == -1||st1[i] == st2[j]){
			i++;
			j++;
		}else{
			j = next[j];
		}
	}
	if(j == len2){
		return i - len2;
	}
	return -1;
}

int main()
{
	int i,j,k,n,cas;
	while(gets(st))
	{
		int len=strlen(st),yunxing=0;
		for(i=len/3;i>=1;i--)
		{
			memset(st2,0,sizeof(st2));
			strncpy(st2,st,i);
			GetNext(st2);
			j=i;
			//printf("%s\n",st2);
			memset(st1,0,sizeof(st1));
			for(k=0;j<len;k++,j++)
			st1[k]=st[j];
			st1[k]='\0';
			//printf("%s\n",st1);
			cas=KMP(st1,st2,0);
			//printf("%d %d\n",cas,len/3);
			if((cas!=-1)&&((cas+i)<=len-2*i))
			{
				cas=KMP(st,st2,len-i);
				if((cas==len-i)&&(cas!=-1))
				{
					yunxing=1;
					printf("%s\n",st2);
					break;
				}
			}
		}
		if(!yunxing) printf("Just a legend\n");
	}
	return 0;
}


/*
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2012年3月25日, 下午1:29
 */

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <set>
#include <iostream>
#include <cstring>
//#define out(x) (std::cerr << #x << ": " << (x) << std::endl)
#define out(...)
using namespace std;
char tmp[8000050];

template <typename _Tp>inline bool Max(_Tp &a, const _Tp &b) {
	return a < b ? a = b, true : false;
}

template <typename _Tp>inline bool Min(_Tp &a, const _Tp &b) {
	return a > b ? a = b, true : false;
}

int getnext(char *pat, int *next) {
	int i, j, len = strlen(pat);
	next[i = 0] = j = -1;
	while (i < len) {
		if (j < 0 || pat[i] == pat[j]) {
			bool b = pat[++i] != pat[++j];
			int yyy = b ? j : next[j];
			next[i] = yyy;
		} else j = next[j];
	}
	int nextlen = next[len];
	bool ret = false;
	next[i = 0] = j = -1;
	while (i < len) {
		if (j < 0 || pat[i] == pat[j]) {
			bool b = pat[++i] != pat[++j];
			int yyy = b ? j : next[j];
			next[i] = yyy;
		} else j = next[j];
		if (i < len && j == nextlen) {
			out(j);
			ret = true;
		}
	}
	//    return nextlen;
	return ret ? nextlen : 0;
}

int next[2][1000050];

int main(int argc, char** argv) {
	while (1 == scanf("%s", tmp)) {
		//int len = strlen(tmp);
		int ans = getnext(tmp, next[0]);
		//for (int i = 0; i < len; ++i) {
		//    printf("%d ", next[0][i]);
		//}
		//std::reverse(tmp, tmp + len);
		//Min(ans, getnext(tmp, next[1]));
		tmp[ans] = 0;
		puts(ans ? tmp : "Just a legend");
	}
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char a[100006];
int len,next[100006],mak[100006];
int main()
{
	int i,j;
	cin>>a+1;
	memset(mak,0,sizeof(mak));
	len=strlen(a+1);
	next[1]=0;j=0;i=1;mak[0]=1;
	while(i<len)
	{
		if(j==0||a[i]==a[j]){i++;j++;next[i]=j;if(i!=len)mak[j]=1;}
		else j=next[j];
	}
//	for(i=0;i<=len;i++)
	//cout<<next[i]<<"   ";
	int h=next[len];
	while(1)
	{
		if(mak[h])break;
		else h=next[h];
	}
//	cout<<h<<endl;
	if(h<1)cout<<"Just a legend\n";
	else
	{
	    for(i=1;i<=h;i++)
         	cout<<a[i];
	           cout<<endl;
	}

}


#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

const int N=1000010;
int n, next[N], c[N];
char s[N];

void getnext(char *s, int n)
{
    int i, k;
   k = -1;
   i = 0;
   next[i] = -1;
    while(i<n)
   {
        if(k==-1 || s[i]==s[k])
       {
           next[++i] = ++k;
        }
        else
            k = next[k];
    }
}

int main()
{
    int i, j, k, ans;
    while(scanf("%s", s)!=EOF)
    {
        n = strlen(s);
        getnext(s, n);
  		//for(i=1;i<=n;i++)
  		//printf("%d ",next[i]);
  		//printf("\n");
        for(i=1; i<=n; i++)
            c[i] = 0;
        for(i=n-1; i>=1; i--)
        {
            c[next[i]] += c[i]+1;
        }
  		//for(i=1;i<=n;i++)
  		//printf("%d ",c[i]);
  		//printf("\n");
        j = next[n];
        while(j!=0)
        {
            if(c[j]>=1)
                break;
            j = next[j];
        }
        if(j==0)
            printf("Just a legend\n");
        else
        {
            s[j] = 0;
            printf("%s\n", s);
        }
    }
    return 0;
}


/*
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2012年3月25日, 下午1:29
 */

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <set>
#include <iostream>
#include <cstring>
//#define out(x) (std::cerr << #x << ": " << (x) << std::endl)
#define out(...)
using namespace std;
char tmp[8000050];

template <typename _Tp>inline bool Max(_Tp &a, const _Tp &b) {
	return a < b ? a = b, true : false;
}

template <typename _Tp>inline bool Min(_Tp &a, const _Tp &b) {
	return a > b ? a = b, true : false;
}

int getnext(string pat, int *next) {
	int i, j;
	next[i = 0] = j = -1;
	while (i < pat.length()) {
		if (j < 0 || pat[i] == pat[j])
			if (pat[++i] == pat[++j])next[i] = next[j];
			else next[i] = j;
		else j = next[j];
	}
	int nextlen = next[pat.length()];
	string ans = pat.substr(0, nextlen);
	int offset = pat.find(ans, 1);
	if (offset >= 0 && offset + ans.size() < pat.size())return nextlen;
	pat.swap(ans);
	next[i = 0] = j = -1;
	while (i < pat.length()) {
		if (j < 0 || pat[i] == pat[j])
			if (pat[++i] == pat[++j])next[i] = next[j];
			else next[i] = j;
		else j = next[j];
	}
	return nextlen = next[pat.length()];
}

int next[1000050];

int main(int argc, char** argv) {
	while (1 == scanf("%s", tmp)) {
		int ans = getnext(tmp, next);
		tmp[ans] = 0;
		puts(ans ? tmp : "Just a legend");
	}
	return 0;
}

/*
fixprefixsuffix
abcdabc
ababab
abcabcab

*/


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define M 1004000
char next[M];

void getNext(char str[]) {
    int len = strlen(str);
    int i = 0, j = -1;
    next[0] = -1;
    while (i < len) {
        if (j == -1 || str[i] == str[j]) {
            i++;
            j++;
            next[i] = j;
        } else {
            j = next[j];
        }
    }
}
int res[M];

int KMP(char st1[], char st2[]) {
    // cout << st1 << endl << st2 << endl;
    int len1 = strlen(st1);
    int i, j;
    i = j = 0;
    int maxn = 0;
    while (i < len1) {
        if (j == -1 || st1[i] == st2[j]) {
            i++;
            j++;
            res[i] = j;
            //cout << i << " " << j << endl;
        } else {
            j = next[j];
        }
    }
    for (i = 1; i < len1; i++) {
        if (res[i] > maxn)
            maxn = res[i];
    }
    return maxn;
}

int KMP2(char st1[], char st2[]) {
   // cout << st1 << endl << st2 << endl;
    int len1 = strlen(st1);
    int len2 = strlen(st2); //cout<<len1<<" "<<len2<<endl;
    int i, j;
    i = j = 0;
    while (i < len1) {
        if (j == -1 || st1[i] == st2[j]) {
            i++;
            j++;
            res[i] = j;
           // cout << i << " " << j << endl;
        } else {
            j = next[j];
        }
    }
    return res[len1];
}
char str[M];
char st2[M];

int main() {
    int i, j;
    while (scanf("%s", str) != EOF) {
        int len = strlen(str);
        if (len <= 2) {
            puts("Just a legend");
            continue;
        }
        getNext(str);
        int ans = KMP(str + 1, str);
        if (ans <= 0) {
            puts("Just a legend");
            continue;
        }
        for (i = 0; i < ans; i++) {
            st2[i] = str[i];
        }
        st2[ans] = '\0';
        getNext(st2);
        ans = min(ans, KMP2(str + 1, st2));
        if (ans <= 0) {
            puts("Just a legend");
            continue;
        }
        for (i = 0; i < ans; i++) {
            cout << str[i];
        }
        cout << endl;
    }
    return 0;
}

/*
qwertyqwertyqwerty

 */


/*
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2012年3月25日, 下午1:29
 */

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <set>
#include <iostream>
#include <cstring>
//#define out(x) (std::cerr << #x << ": " << (x) << std::endl)
#define out(...)
using namespace std;
char tmp[8000050];

template <typename _Tp>inline bool Max(_Tp &a, const _Tp &b) {
	return a < b ? a = b, true : false;
}

template <typename _Tp>inline bool Min(_Tp &a, const _Tp &b) {
	return a > b ? a = b, true : false;
}

bool eq(const char *a, const char *b, int len) {
	for (int i = 0; i < len; ++i) {
		if (*a++ != *b++)return false;
	}
	return true;
}

int getnext(string pat, int *next) {
	int i, j;
	next[i = 0] = j = -1;
	while (i < pat.length()) {
		if (j < 0 || pat[i] == pat[j])
			if (pat[++i] == pat[++j])next[i] = next[j];
			else next[i] = j;
		else j = next[j];
	}
	int nextlen = next[pat.length()];
	string ans = pat.substr(0, nextlen);
	int offset = pat.find(ans, 1);
	if (offset >= 0 && offset + ans.size() < pat.size())return nextlen;
	for (int i = ans.length() - 1; i > 0; --i) {
		ans[i] = 0;
		offset = pat.find(ans.c_str(), 1);
		if (offset >= 0 && offset + ans.size() < pat.size() &&
				eq(ans.c_str(), pat.c_str() + pat.length() - i, i))return i;
	}
	return 0;
}

int next[1000050];

int main(int argc, char** argv) {
	while (1 == scanf("%s", tmp)) {
		int ans = getnext(tmp, next);
		tmp[ans] = 0;
		puts(ans ? tmp : "Just a legend");
	}
	return 0;
}

/*
fixprefixsuffix
abcdabc
ababab
abcabcab

 */


#include<stdio.h>
#include<algorithm>
#include<string.h>
#define N 1000
using namespace std;
int fail[N],c[N];
char pat[N];
void getnext()
{
	int i,k,n;
	i=0,k=-1;
	n=strlen(pat);
	fail[i]=-1;
	while(i<n)
	{
		if(k==-1||pat[i]==pat[k])
		{
			++i;++k;fail[i]=k;
		}
		else k=fail[k];
	}
}
int main()
{
	int i,j,n,k,ans;
	while(scanf("%s",pat)!=EOF)
	{
		getnext();
		n=strlen(pat);
		for(i=0;i<=n;i++)
		{
			c[i]=0;
		//	printf("%d ",fail[i]);
		}
	//	printf("\n");
		for(i=n;i>=0;i--)
		{
			c[fail[i]]+=c[i]+1;
		}
		j=fail[n];
	//	for(i=0;i<=n;i++)
	//	printf("%d ",c[i]);
	//	printf("\n");
		ans=-1;
		while(j>0)
		{
			//printf("%d %d\n",j,c[j]);
			k=c[j];
			if(k>1)
			{
				ans=j;
				break;
			}
			else j=fail[j];
		}
		if(ans>0)
		{
			for(i=0;i<ans;i++)
			{
				printf("%c",pat[i]);
			}
			printf("\n");
		}
		else printf("Just a legend\n");
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

char src[1000001];
int hash[1000001];
int next[1000001];

void getnext(int length) {
    memset(next, 0, sizeof(next));
    next[0]  = -1;
    int k = -1;
    int j = 0;

    while(j < length){
        if (k == -1 || src[j] == src[k]){
            next[++j] = ++k;
        } else {
            k = next[k];
        }
    }
}

int main(){
    while(~scanf("%s", src)){
        int len;
        memset(hash, 0, sizeof(hash));
        len = strlen(src);
        getnext(len);

        for (int i = 0; i < len; ++i)
            hash[next[i]] = 1;
        hash[0] = 0;

        int flag = 0;
        for (int i = len; i; i = next[i]) {
            if (hash[next[i]]){
                flag = 1;
                for (int j = 0; j < next[i]; ++j)
                    printf("%c", src[j]);
                cout<<endl;
                break;
            }
        }
        if (!flag)
            cout<<"Just a legend"<<endl;
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int *getnext(char *p) {
    int length = strlen(p);
    int *next = new int[length + 1];
    next[0]  = -1;
    int k = -1;
    int j = 0;

    while(j < length){
        if (k == -1 || p[j] == p[k]){
            next[++j] = ++k;
        } else {
            k = next[k];
        }
    }

    return next;
}
char src[1000001];
int hash[1000001];

int main(){
    while(~scanf("%s", src)){
        memset(hash, 0, sizeof(hash));
        int *next = getnext(src);

        for (int i = 0; i < strlen(src); ++i)
            hash[next[i]] = 1;
        hash[0] = 0;

        int flag = 0;
        for (int i = strlen(src); i; i = next[i]) {
            if (hash[next[i]]){
                flag = 1;
                for (int j = 0; j < next[i]; ++j)
                    printf("%c", src[j]);
                cout<<endl;
                break;
            }
        }
        if (!flag)
            cout<<"Just a legend"<<endl;

        delete [] next;
    }
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int *getnext(char *p, int len){
    int length = len;
    int *next = new int[length];
    next[0] = -1;
    int j = 0;
    int k = -1;

    while(j < length - 1) {
        if (k == -1 || p[j] == p[k]) {
            if (p[++j] == p[++k])
                next[j] = next[k];
            else
                next[j] = k;
        } else {
            k = next[k];
        }
    }

    return next;
}

int pipei(char *src, char *substring, int *next, int len) {
    int length1 = strlen(src) - 1;
    int length2 = len;
    int i = 1;
    int j = 0;

    while(i < length1 && j < length2) {
        if (j == -1 || src[i] == substring[j]) {
            ++i;
            ++j;
        } else {
            j = next[j];
        }
    }

    if (j == length2)
        return 1;
    else
        return 0;
}

int main(){
    char src[1000001];
    char substring[500001];
    while(scanf("%s", src) != EOF) {
        int len = strlen(src);
        int key = 0;
        for (int i = len - 2; i >= 1 ; --i){
            memcpy(substring, src, i);
            int temp = 0;
            int k = 0;
            for (int j = len - i; j <= len - 1; ++j){
                if (substring[k++] != src[j]){
                    temp = 1;
                    break;
                }
            }

            if (!temp){

                int *next = getnext(substring, i);
                int result = pipei(src, substring, next, i);
                if (result){
                    key = i;
                    delete [] next;
                    break;
                }

            }
        }

        if (key){
            for(int i = 0; i < key; ++i)
                cout<<src[i];
            cout<<endl;
        } else
            cout<<"Just a legend"<<endl;
    }

    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;

int *getnext(char *p, int len){
    int length = len;
    int *next = new int[length];
    next[0] = -1;
    int j = 0;
    int k = -1;

    while(j < length - 1) {
        if (k == -1 || p[j] == p[k]) {
            if (p[++j] == p[++k])
                next[j] = next[k];
            else
                next[j] = k;
        } else {
            k = next[k];
        }
    }

    return next;
}

int pipei(char *src, char *substring, int *next, int len) {
    int length1 = strlen(src) - 1;
    int length2 = len;
    int i = 1;
    int j = 0;

    while(i < length1 && j < length2) {
        if (j == -1 || src[i] == substring[j]) {
            ++i;
            ++j;
        } else {
            j = next[j];
        }
    }

    if (j == length2)
        return 1;
    else
        return 0;
}

int main(){
    char src[1000001];
    char substring[500001];
    while(cin.getline(src, 1000001)) {
        int len = strlen(src);
        int key = 0;
        for (int i = 1; i < len - 1; ++i){
            memcpy(substring, src, i);
            int temp = 0;
            int k = 0;
            for (int j = len - i; j <= len - 1; ++j){
                if (substring[k++] != src[j]){
                    temp = 1;
                    break;
                }
            }

            if (!temp){

                int *next = getnext(substring, i);
                int result = pipei(src, substring, next, i);
                if (result)
                    key = i;
                delete [] next;
            }
        }

        if (key){
            for(int i = 0; i < key; ++i)
                cout<<src[i];
            cout<<endl;
        } else
            cout<<"Just a legend"<<endl;
    }

    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;

int *getnext(char *p, int len){
    int length = len;
    int *next = new int[length];
    next[0] = -1;
    int j = 0;
    int k = -1;

    while(j < length - 1) {
        if (k == -1 || p[j] == p[k]) {
            if (p[++j] == p[++k])
                next[j] = next[k];
            else
                next[j] = k;
        } else {
            k = next[k];
        }
    }

    return next;
}

int pipei(char *src, char *substring, int *next, int len) {
    int length1 = strlen(src) - 1;
    int length2 = len;
    int i = 1;
    int j = 0;

    while(i < length1 && j < length2) {
        if (j == -1 || src[i] == substring[j]) {
            ++i;
            ++j;
        } else {
            j = next[j];
        }
    }

    if (j == length2)
        return 1;
    else
        return 0;
}

int main(){
    char src[1000001];
    char substring[500001];
    while(cin.getline(src, 1000001)) {
        int len = strlen(src);
        int key = 0;
        for (int i = len - 2; i >= 1 ; --i){
            memcpy(substring, src, i);
            int temp = 0;
            int k = 0;
            for (int j = len - i; j <= len - 1; ++j){
                if (substring[k++] != src[j]){
                    temp = 1;
                    break;
                }
            }

            if (!temp){

                int *next = getnext(substring, i);
                int result = pipei(src, substring, next, i);
                if (result){
                    key = i;
                    delete [] next;
                    break;
                }

            }
        }

        if (key){
            for(int i = 0; i < key; ++i)
                cout<<src[i];
            cout<<endl;
        } else
            cout<<"Just a legend"<<endl;
    }

    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int *getnext(char *p, int len){
    int length = len;
    int *next = new int[length];
    next[0] = -1;
    int j = 0;
    int k = -1;

    while(j < length - 1) {
        if (k == -1 || p[j] == p[k]) {
            if (p[++j] == p[++k])
                next[j] = next[k];
            else
                next[j] = k;
        } else {
            k = next[k];
        }
    }

    return next;
}

int pipei(char *src, char *substring, int *next, int len) {
    int length1 = strlen(src) - 1;
    int length2 = len;
    int i = 1;
    int j = 0;

    while(i < length1 && j < length2) {
        if (j == -1 || src[i] == substring[j]) {
            ++i;
            ++j;
        } else {
            j = next[j];
        }
    }

    if (j == length2)
        return 1;
    else
        return 0;
}

int main(){
    char src[1000001];
    char substring[500001];
    while(scanf("%s", src)) {
        int len = strlen(src);
        int key = 0;
        for (int i = len - 2; i >= 1 ; --i){
            memcpy(substring, src, i);
            int temp = 0;
            int k = 0;
            for (int j = len - i; j <= len - 1; ++j){
                if (substring[k++] != src[j]){
                    temp = 1;
                    break;
                }
            }

            if (!temp){

                int *next = getnext(substring, i);
                int result = pipei(src, substring, next, i);
                if (result){
                    key = i;
                    delete [] next;
                    break;
                }

            }
        }

        if (key){
            for(int i = 0; i < key; ++i)
                cout<<src[i];
            cout<<endl;
        } else
            cout<<"Just a legend"<<endl;
    }

    return 0;
}


#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000010
int n;
int nxt[N],used[N];
char s[N];
void Init()
{
    int j=0;
    for(int i=2;i<=n;i++)
    {
        while(j&&s[i]!=s[j+1]) j=nxt[j];
        if(s[i]==s[j+1]) j++;
        nxt[i]=j;
    }
}
int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    Init();
    for(int i=1;i<n;i++) used[nxt[i]]=1;
    used[0]=0;
    for(int i=n;i;i=nxt[i]) if(used[nxt[i]])
    {
        for(int j=1;j<=nxt[i];j++) printf("%c",s[j]);
        return 0;
    }
    printf("Just a legend");
    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;

int *getnext(char *p, int len){
    int length = len;
    int *next = new int[length];
    next[0] = -1;
    int j = 0;
    int k = -1;

    while(j < length - 1) {
        if (k == -1 || p[j] == p[k]) {
            if (p[++j] == p[++k])
                next[j] = next[k];
            else
                next[j] = k;
        } else {
            k = next[k];
        }
    }

    return next;
}

int pipei(char *src, char *substring, int *next, int len) {
    int length1 = strlen(src) - 1;
    int length2 = len;
    int i = 1;
    int j = 0;

    while(i < length1 && j < length2) {
        if (j == -1 || src[i] == substring[j]) {
            ++i;
            ++j;
        } else {
            j = next[j];
        }
    }

    if (j == length2)
        return 1;
    else
        return 0;
}

int main(){
    char src[1000001];
    char substring[500001];
    while(cin.getline(src, 1000001)) {
        int len = strlen(src);
        int key = 0;
        for (int i = 1; i <= len / 2; ++i){
            memcpy(substring, src, i);
            int temp = 0;
            int k = 0;
            for (int j = len - i; j <= len - 1; ++j){
                if (substring[k++] != src[j]){
                    temp = 1;
                    break;
                }
            }

            if (!temp){

                int *next = getnext(substring, i);
                int result = pipei(src, substring, next, i);
                if (result)
                    key = i;
                delete [] next;
            }
        }

        if (key){
            for(int i = 0; i < key; ++i)
                cout<<src[i];
            cout<<endl;
        } else
            cout<<"Just a legend"<<endl;
    }

    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int *getnext(char *p) {
    int length = strlen(p);
    int *next = new int[length + 1];
    next[0]  = -1;
    int k = -1;
    int j = 0;

    while(j < length){
        if (k == -1 || p[j] == p[k]){
            next[++j] = ++k;
        } else {
            k = next[k];
        }
    }

    return next;
}
char src[1000001];
int hash[1000001];

int main(){
    while(cin>>src){
        memset(hash, 0, sizeof(hash));
        int *next = getnext(src);

        for (int i = 0; i < strlen(src); ++i)
            hash[next[i]] = 1;
        hash[0] = 0;

        int flag = 0;
        for (int i = strlen(src); i; i = next[i]) {
            if (hash[next[i]]){
                flag = 1;
                for (int j = 0; j < next[i]; ++j)
                    cout<<src[j];
                cout<<endl;
                break;
            }
        }
        if (!flag)
            cout<<"Just a legend"<<endl;

        delete [] next;
    }
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
          while (r<n && a[r]==a[r-l])r++;
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-i+1)
             z[i] = z[k];
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l;
                r--;
            }
        }
    }
    /*
 int maxz = 0, res = 0;
    int ot = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
*/
    int ot = 0;
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
        ans = n-i;  ot = 1;break;
        }
         ans = max(ans,z[i]);
    }
    a[ans] = 0;
    if(ot)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
          while (r<n && a[r]==a[r-l])r++;
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-l+1)
             z[i] = z[k];
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l;
                r--;
            }
        }
    }
    /*
    int L = 0, R = 0;
for (int i = 1; i < n; i++) {
  if (i > R) {
    L = R = i;
    while (R < n && a[R-L] == a[R]) R++;
    z[i] = R-L; R--;
  } else {
    int k = i-L;
    if (z[k] < R-i+1) z[i] = z[k];
    else {
      L = i;
      while (R < n && a[R-L] == a[R]) R++;
      z[i] = R-L; R--;
    }
  }
}
*/
    int maxz = 0, res = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
           ans = n-i;break;
        }
         ans = max(ans,z[i]);
    }
    a[res] = 0;
    if(res)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
          while (r<n && a[r]==a[r-l])r++;
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-i+1)
             z[i] = z[k];
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l;
                r--;
            }
        }
    }
    /*
    int L = 0, R = 0;
for (int i = 1; i < n; i++) {
  if (i > R) {
    L = R = i;
    while (R < n && a[R-L] == a[R]) R++;
    z[i] = R-L; R--;
  } else {
    int k = i-L;
    if (z[k] < R-i+1) z[i] = z[k];
    else {
      L = i;
      while (R < n && a[R-L] == a[R]) R++;
      z[i] = R-L; R--;
    }
  }
}
*/
    int maxz = 0, res = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
           ans = n-i;break;
        }
         ans = max(ans,z[i]);
    }
    a[res] = 0;
    if(res)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
            while(r<n)
                {
                if(a[r-l]!=a[r])break;
                r++;
            }
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-l+1)
                {
                z[i] = z[k];
            }
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l+1;
                r--;
            }
        }
    }
    int ans = 0;
    for(i=1;i<n;i++)
      {
        if(z[i]==n-i){
            if(z[i]>ans)ans = z[i];
        }
    }
    if(ans)printf("%d\n",ans);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
            while(r<n)
                {
                if(a[r-l]!=a[r])break;
                r++;
            }
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-l+1)
                {
                z[i] = z[k];
            }
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l+1;
                r--;
            }
        }
    }
    int maxz = 0, res = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
           ans = n-i;break;
        }
         ans = max(ans,z[i]);
    }
    a[res] = 0;
    if(res)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
            while(r<n)
                {
                if(a[r-l]!=a[r])break;
                r++;
            }
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-l+1)
                {
                z[i] = z[k];
            }
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l+1;
                r--;
            }
        }
    }

    int L = 0, R = 0;
for (int i = 1; i < n; i++) {
  if (i > R) {
    L = R = i;
    while (R < n && a[R-L] == a[R]) R++;
    z[i] = R-L; R--;
  } else {
    int k = i-L;
    if (z[k] < R-i+1) z[i] = z[k];
    else {
      L = i;
      while (R < n && a[R-L] == a[R]) R++;
      z[i] = R-L; R--;
    }
  }
}

    int maxz = 0, res = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
           ans = n-i;break;
        }
         ans = max(ans,z[i]);
    }
    a[res] = 0;
    if(res)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
            while(r<n)
                {
                if(a[r-l]!=a[r])break;
                r++;
            }
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-l+1)
                {
                z[i] = z[k];
            }
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l;
                r--;
            }
        }
    }
    /*
    int L = 0, R = 0;
for (int i = 1; i < n; i++) {
  if (i > R) {
    L = R = i;
    while (R < n && a[R-L] == a[R]) R++;
    z[i] = R-L; R--;
  } else {
    int k = i-L;
    if (z[k] < R-i+1) z[i] = z[k];
    else {
      L = i;
      while (R < n && a[R-L] == a[R]) R++;
      z[i] = R-L; R--;
    }
  }
}
*/
    int maxz = 0, res = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
           ans = n-i;break;
        }
         ans = max(ans,z[i]);
    }
    a[res] = 0;
    if(res)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
          while (r<n && a[r]==a[r-l])r++;
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-i+1)
             z[i] = z[k];
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l;
                r--;
            }
        }
    }
    /*
 int maxz = 0, res = 0;
    int ot = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
*/
    int ot = 0;
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
          ot = 1;break;
        }
         ans = max(ans,z[i]);
    }
    a[ans] = 0;
    if(ot)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include<bits/stdc++.h>
#include<string.h>
using namespace std;
char a[1000056];
int z[1000056];
int in = 0;
int main()
    {
scanf("%s",&a);
    int l,r;
    l = r = 0;
    int i;
    int k;
    int n = strlen(a);
    for(i=1;i<n;i++)
        {
        if(i>r){
            l = r = i;
            while(r<n)
                {
                if(a[r-l]!=a[r])break;
                r++;
            }
            z[i] = r-l;r--;
        }
        else {
            k = i-l;
            if(z[k]<r-l+1)
                {
                z[i] = z[k];
            }
            else {
                l = i;
                while (r<n && a[r]==a[r-l])r++;
                z[i]  = r-l;
                r--;
            }
        }
    }

    int L = 0, R = 0;
for (int i = 1; i < n; i++) {
  if (i > R) {
    L = R = i;
    while (R < n && a[R-L] == a[R]) R++;
    z[i] = R-L; R--;
  } else {
    int k = i-L;
    if (z[k] < R-i+1) z[i] = z[k];
    else {
      L = i;
      while (R < n && a[R-L] == a[R]) R++;
      z[i] = R-L; R--;
    }
  }
}

    int maxz = 0, res = 0;
for (int i = 1; i < n; i++) {
  if (z[i] == n-i && maxz >= n-i) { res = n-i; break; }
  maxz = max(maxz, z[i]);
}
    int ans = 0;
    for(i=1;i<n;i++)
      {
     //   printf("%d is %d\n",i,z[i]);
        if(z[i]==n-i &&ans>=n-i){
           ans = n-i;break;
        }
         ans = max(ans,z[i]);
    }
    a[res] = 0;
    if(res)printf("%s\n",a);
    else printf("Just a legend\n");
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
//    for (int i = 0; i < pi.size(); ++i)
//        cout << pi[i] << " ";
//    cout << endl;
    int n = s.length()-1;
    int len = pi[n];
    if (cnt[len] == 1)
    {
        if (pi[len] == 0)
            {
                cout << "Just a legend";
                return 0;
            }
        len = pi[len]-1;
        while (!cnt[len])
        {
            if (pi[len] == 0)
            {
                cout << "Just a legend";
                return 0;
            }
            len = pi[len]-1;
        }
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
    int n = s.length()-1;
    int len = pi[n];
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }
    for (int i = 0; i < n-1; ++i)
        if (pi[i] == len)
    {
        cout << s.substr(0,len);
        return 0;
    }
    len = pi[len-1];
    cout << len << endl;
    if (len == 0)
    {
        cout << "Just a legend";
        return 0;
    }
    cout << s.substr(0,len);
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int cnt[3000000];

vector<int> p_fun(string s)
{
    int n = s.length();
    vector <int> pi(n);
    for (int i = 1; i < n; ++i)
    {
        int j = pi[i-1];
        while (s[i] != s[j] && j > 0) j = pi[j-1];
        if (s[i] == s[j]) j++;
        pi[i] = j++;
        cnt[j]++;
    }
    return pi;
}

int main()
{
    cin >> s;
    vector <int> pi = p_fun(s);
//    for (int i = 0; i < pi.size(); ++i)
//        cout << pi[i] << " ";
//    cout << endl;
    int n = s.length()-1;
    int len = pi[n];
    if (cnt[len] <= 1)
    {
        if (pi[len] == 0)
            {
                cout << "Just a legend";
                return 0;
            }
        len = pi[len]-1;
        while (!cnt[len])
        {
            if (pi[len] == 0)
            {
                cout << "Just a legend";
                return 0;
            }
            len = pi[len]-1;
        }
    }
    cout << s.substr(0,len);
    return 0;
}