import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
import java.math.BigInteger;

public class Password{
	public static FastReader fs = new FastReader();
	static final long p = 31l;
	static final long mod = 1_000_000_007;
	static char s [];
	static long hash [];
	static PrintWriter out = new PrintWriter(System.out);

	interface hashing{
		boolean check(int len);
	}

	static void solve() {
		StringBuffer output = new StringBuffer();
		s = fs.next().toCharArray();
		int n = s.length;
		hash = new long [n];
		hash[0] = s[0]-'a'+1;
		for(int i = 1; i<n; i++) {
			hash[i] = (hash[i-1]*p+s[i]-'a'+1)%mod;
		}
		ArrayList<Integer> matched = new ArrayList<>();
		for(int i = n-2; i>=1; i--) {
			long prehash = hash[i-1];
			long suffhash = ((hash[n-1] - (hash[n-i-1]*power(p,i,mod))%mod)%mod+mod)%mod;
			if(prehash == suffhash)
				matched.add(i);
		}

		hashing hs = new hashing() {
			public boolean check(int len) {
				for(int i = 1; i+len<n; i++) {
					long hashmid = ((hash[i+len-1] - (hash[i-1]*power(p,len,mod))%mod)%mod+mod)%mod;
					if(hashmid == hash[len-1])return true;
				}
				return false;
			}
		};
		Collections.sort(matched);
		if(matched.isEmpty())
			out.println("Just a legend");
		else {
			//out.println(1);
			//for(int x : matched)
				//out.println(x);
			int start = 0,end = matched.size()-1;
			int ans = -1;
			while(start <= end) {
				int mid = start + (end - start) / 2;
				if(hs.check(matched.get(mid))) {
					ans = matched.get(mid);
					start = mid+1;
				}
				else
					end = mid-1;
			}
			if(ans == -1) {
				out.println("Just a legend");
				return;
			}
			for(int i = 0; i<ans; i++)
				out.print(s[i]);
			out.println();
		}
	}

	public static void main(String[] args) {
		Thread t = new Thread(null,null,"",1 << 28) {
			public void run() {
				int test_case = 1;
				//test_case = fs.nextInt();
				for(int cs = 1; cs <= test_case; cs++) {
					solve();
				}
				out.close();
			}
		};
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

	static int ceil(int x,int y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static long ceil(long x,long y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static int max(int x,int y) {
		return Math.max(x, y);
	}

	static int min(int x,int y) {
		return Math.min(x, y);
	}

	static long max(long x,long y) {
		return Math.max(x, y);
	}

	static long min(long x,long y) {
		return Math.min(x, y);
	}

	static int min(int a []) {
		int x = 1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static int max(int a []) {
		int x = -1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static long min(long a []) {
		long x = (long)3e18;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static long max(long a []) {
		long x = -(long)3e18;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static int power(int x,int y) {
		int res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y) {
		long res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y,long mod) {
		long res = 1;
		x %= mod;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x) % mod;
			y >>= 1;
            x = (x * x) % mod;
		}
		return res;
	}

	static void intsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverseintsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverselongsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void intpairsort(intpair [] a) {
		List<intpair> temp = new ArrayList<intpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longpairsort(longpair [] a) {
		List<longpair> temp = new ArrayList<longpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static class intpair{
		public int x;
		public int y;

		intpair(int a,int b){
			this.x = a;
			this.y = b;
		}

		intpair(){}
	}

	static class longpair{
		public long x;
		public long y;

		longpair(long a,long b){
			this.x = a;
			this.y = b;
		}

		longpair(){}
	}

	static class data{
		public long sum;

		data(long val){
		    this.sum = val;
		}

		data(){}

		data combine(data l, data r) {
		    data res = new data();
		    res.sum = l.sum + r.sum;
		    return res;
		}
	}

	static class Seg_Tree extends data{
		public int n;
		data [] seg;

		Seg_Tree(int sz){
			this.n = sz;
			seg = new data[4*n+4];
		}

		void build(long a[], int v, int tl, int tr) {
		    if (tl == tr) {
		        seg[v] = new data(a[tl]);
		    } else {
		        int tm = (tl + tr) / 2;
		        build(a, v*2, tl, tm);
		        build(a, v*2+1, tm+1, tr);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		void update(int v, int tl, int tr, int pos, long new_val) {
		    if (tl == tr) {
		        seg[v] = new data(new_val);
		    } else {
		        int tm = (tl + tr) / 2;
		        if (pos <= tm)
		            update(v*2, tl, tm, pos, new_val);
		        else
		            update(v*2+1, tm+1, tr, pos, new_val);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		data query(int v, int tl, int tr, int l, int r) {
		    if (l > r)
		        return new data(0);
		    if (l == tl && r == tr)
		        return seg[v];
		    int tm = (tl + tr) / 2;
		    return combine(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r));
		}
	}

	static class Bit_Tree{
		static int n;
		static int [] bit;

		Bit_Tree(int sz){
			Bit_Tree.n = sz;
			Bit_Tree.bit = new int[n+1];
		}

		static int child(int x) {
			return x + (x & (-x));
		}

		static int parent(int x) {
			return x - (x & (-x));
		}

		static void build(int [] a) {
			for(int i = 0; i<a.length; i++) {
				int start = i+1;
				while(start <= n) {
					bit[start] += a[i];
					start = child(start);
				}
			}
		}

		static void update(int idx,int new_val) {
			idx += 1;
			while(idx <= n) {
				bit[idx] += new_val;
				idx = child(idx);
			}
		}

		static int query(int right) {
			int res = 0;
			while(right > 0) {
				res += bit[right];
				right = parent(right);
			}
			return res;
		}

		static int query(int left,int right) {
			return query(right) - query(left-1);
		}
	}

	public static int gcd(int a, int b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.intValue();
    }

    public static long gcd(long a, long b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.longValue();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
import java.math.BigInteger;

public class Password{
	public static FastReader fs = new FastReader();
	static final long p = 31l;
	static final long mod = 1_000_000_007;
	static char s [];
	static long hash [];
	static long powers [];
	static int n;
	static PrintWriter out = new PrintWriter(System.out);

	static boolean check(int len) {
		for(int i = 1; i+len<n; i++) {
			long hashmid = (hash[i+len-1] - hash[i-1]*powers[i])%mod;
			if(hashmid < 0)hashmid += mod;
			if(hashmid == hash[len-1])return true;
		}
		return false;
	}
	static void solve() {
		StringBuffer output = new StringBuffer();
		s = fs.next().toCharArray();
		n = s.length;
		hash = new long [n];
		powers = new long [n+1];
		hash[0] = s[0]-'a'+1;
		for(int i = 1; i<n; i++) {
			hash[i] = (hash[i-1]*p+s[i]-'a'+1)%mod;
			powers[i] = power(p,i,mod);
		}
		ArrayList<Integer> matched = new ArrayList<>();
		for(int i = 1; i<=n-2; i++) {
			long prehash = hash[i-1];
			long suffhash = (hash[n-1] - (hash[n-i-1]*powers[i]))%mod;
			if(suffhash < 0)suffhash += mod;
			if(prehash == suffhash)
				matched.add(i);
		}

		//Collections.sort(matched);
		if(matched.isEmpty())
			out.println("Just a legend");
		else {
			//out.println(1);
			//for(int x : matched)
				//out.println(x);
			int start = 0,end = matched.size()-1;
			int ans = -1;
			while(start <= end) {
				int mid = start + (end - start) / 2;
				if(check(matched.get(mid))) {
					ans = matched.get(mid);
					start = mid+1;
				}
				else
					end = mid-1;
			}
			if(ans == -1) {
				out.println("Just a legend");
				return;
			}
			for(int i = 0; i<ans; i++)
				out.print(s[i]);
			out.println();
		}
	}

	public static void main(String[] args) {
		Thread t = new Thread(null,null,"",1 << 28) {
			public void run() {
				int test_case = 1;
				//test_case = fs.nextInt();
				for(int cs = 1; cs <= test_case; cs++) {
					solve();
				}
				out.close();
			}
		};
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

	static int ceil(int x,int y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static long ceil(long x,long y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static int max(int x,int y) {
		return Math.max(x, y);
	}

	static int min(int x,int y) {
		return Math.min(x, y);
	}

	static long max(long x,long y) {
		return Math.max(x, y);
	}

	static long min(long x,long y) {
		return Math.min(x, y);
	}

	static int min(int a []) {
		int x = 1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static int max(int a []) {
		int x = -1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static long min(long a []) {
		long x = (long)3e18;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static long max(long a []) {
		long x = -(long)3e18;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static int power(int x,int y) {
		int res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y) {
		long res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y,long mod) {
		long res = 1;
		x %= mod;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x) % mod;
			y >>= 1;
            x = (x * x) % mod;
		}
		return res;
	}

	static void intsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverseintsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverselongsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void intpairsort(intpair [] a) {
		List<intpair> temp = new ArrayList<intpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longpairsort(longpair [] a) {
		List<longpair> temp = new ArrayList<longpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static class intpair{
		public int x;
		public int y;

		intpair(int a,int b){
			this.x = a;
			this.y = b;
		}

		intpair(){}
	}

	static class longpair{
		public long x;
		public long y;

		longpair(long a,long b){
			this.x = a;
			this.y = b;
		}

		longpair(){}
	}

	static class data{
		public long sum;

		data(long val){
		    this.sum = val;
		}

		data(){}

		data combine(data l, data r) {
		    data res = new data();
		    res.sum = l.sum + r.sum;
		    return res;
		}
	}

	static class Seg_Tree extends data{
		public int n;
		data [] seg;

		Seg_Tree(int sz){
			this.n = sz;
			seg = new data[4*n+4];
		}

		void build(long a[], int v, int tl, int tr) {
		    if (tl == tr) {
		        seg[v] = new data(a[tl]);
		    } else {
		        int tm = (tl + tr) / 2;
		        build(a, v*2, tl, tm);
		        build(a, v*2+1, tm+1, tr);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		void update(int v, int tl, int tr, int pos, long new_val) {
		    if (tl == tr) {
		        seg[v] = new data(new_val);
		    } else {
		        int tm = (tl + tr) / 2;
		        if (pos <= tm)
		            update(v*2, tl, tm, pos, new_val);
		        else
		            update(v*2+1, tm+1, tr, pos, new_val);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		data query(int v, int tl, int tr, int l, int r) {
		    if (l > r)
		        return new data(0);
		    if (l == tl && r == tr)
		        return seg[v];
		    int tm = (tl + tr) / 2;
		    return combine(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r));
		}
	}

	static class Bit_Tree{
		static int n;
		static int [] bit;

		Bit_Tree(int sz){
			Bit_Tree.n = sz;
			Bit_Tree.bit = new int[n+1];
		}

		static int child(int x) {
			return x + (x & (-x));
		}

		static int parent(int x) {
			return x - (x & (-x));
		}

		static void build(int [] a) {
			for(int i = 0; i<a.length; i++) {
				int start = i+1;
				while(start <= n) {
					bit[start] += a[i];
					start = child(start);
				}
			}
		}

		static void update(int idx,int new_val) {
			idx += 1;
			while(idx <= n) {
				bit[idx] += new_val;
				idx = child(idx);
			}
		}

		static int query(int right) {
			int res = 0;
			while(right > 0) {
				res += bit[right];
				right = parent(right);
			}
			return res;
		}

		static int query(int left,int right) {
			return query(right) - query(left-1);
		}
	}

	public static int gcd(int a, int b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.intValue();
    }

    public static long gcd(long a, long b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.longValue();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
import java.math.BigInteger;

public class Password{
	public static FastReader fs = new FastReader();
	static final long p = 31l;
	static final long mod = 1_000_000_007;
	static char s [];
	static long hash [];
	static PrintWriter out = new PrintWriter(System.out);

	interface hashing{
		boolean check(int len);
	}

	static void solve() {
		StringBuffer output = new StringBuffer();
		s = fs.next().toCharArray();
		int n = s.length;
		hash = new long [n];
		hash[0] = s[0]-'a'+1;
		for(int i = 1; i<n; i++) {
			hash[i] = (hash[i-1]*p+s[i]-'a'+1)%mod;
		}
		ArrayList<Integer> matched = new ArrayList<>();
		for(int i = n-2; i>=1; i--) {
			long prehash = hash[i-1];
			long suffhash = ((hash[n-1] - (hash[n-i-1]*power(p,i,mod))%mod)%mod+mod)%mod;
			if(prehash == suffhash)
				matched.add(i);
		}

		hashing hs = new hashing() {
			public boolean check(int len) {
				for(int i = 1; i+len<n; i++) {
					long hashmid = ((hash[i+len-1] - (hash[i-1]*power(p,len,mod))%mod)%mod+mod)%mod;
					if(hashmid == hash[len-1])return true;
				}
				return false;
			}
		};

		if(matched.isEmpty())
			out.println("Just a legend");
		else {
			int start = 0,end = matched.size()-1;
			int ans = -1;
			while(start <= end) {
				int mid = start + (end - start) / 2;
				if(hs.check(matched.get(mid))) {
					ans = matched.get(mid);
					start = mid+1;
				}
				else
					end = mid-1;
			}
			if(ans == -1) {
				out.println("Just a legend");
				return;
			}
			for(int i = 0; i<ans; i++)
				out.print(s[i]);
			out.println();
		}
	}

	public static void main(String[] args) {
		Thread t = new Thread(null,null,"",1 << 28) {
			public void run() {
				int test_case = 1;
				//test_case = fs.nextInt();
				for(int cs = 1; cs <= test_case; cs++) {
					solve();
				}
				out.close();
			}
		};
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

	static int ceil(int x,int y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static long ceil(long x,long y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static int max(int x,int y) {
		return Math.max(x, y);
	}

	static int min(int x,int y) {
		return Math.min(x, y);
	}

	static long max(long x,long y) {
		return Math.max(x, y);
	}

	static long min(long x,long y) {
		return Math.min(x, y);
	}

	static int min(int a []) {
		int x = 1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static int max(int a []) {
		int x = -1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static long min(long a []) {
		long x = (long)3e18;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static long max(long a []) {
		long x = -(long)3e18;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static int power(int x,int y) {
		int res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y) {
		long res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y,long mod) {
		long res = 1;
		x %= mod;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x) % mod;
			y >>= 1;
            x = (x * x) % mod;
		}
		return res;
	}

	static void intsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverseintsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverselongsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void intpairsort(intpair [] a) {
		List<intpair> temp = new ArrayList<intpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longpairsort(longpair [] a) {
		List<longpair> temp = new ArrayList<longpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static class intpair{
		public int x;
		public int y;

		intpair(int a,int b){
			this.x = a;
			this.y = b;
		}

		intpair(){}
	}

	static class longpair{
		public long x;
		public long y;

		longpair(long a,long b){
			this.x = a;
			this.y = b;
		}

		longpair(){}
	}

	static class data{
		public long sum;

		data(long val){
		    this.sum = val;
		}

		data(){}

		data combine(data l, data r) {
		    data res = new data();
		    res.sum = l.sum + r.sum;
		    return res;
		}
	}

	static class Seg_Tree extends data{
		public int n;
		data [] seg;

		Seg_Tree(int sz){
			this.n = sz;
			seg = new data[4*n+4];
		}

		void build(long a[], int v, int tl, int tr) {
		    if (tl == tr) {
		        seg[v] = new data(a[tl]);
		    } else {
		        int tm = (tl + tr) / 2;
		        build(a, v*2, tl, tm);
		        build(a, v*2+1, tm+1, tr);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		void update(int v, int tl, int tr, int pos, long new_val) {
		    if (tl == tr) {
		        seg[v] = new data(new_val);
		    } else {
		        int tm = (tl + tr) / 2;
		        if (pos <= tm)
		            update(v*2, tl, tm, pos, new_val);
		        else
		            update(v*2+1, tm+1, tr, pos, new_val);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		data query(int v, int tl, int tr, int l, int r) {
		    if (l > r)
		        return new data(0);
		    if (l == tl && r == tr)
		        return seg[v];
		    int tm = (tl + tr) / 2;
		    return combine(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r));
		}
	}

	static class Bit_Tree{
		static int n;
		static int [] bit;

		Bit_Tree(int sz){
			Bit_Tree.n = sz;
			Bit_Tree.bit = new int[n+1];
		}

		static int child(int x) {
			return x + (x & (-x));
		}

		static int parent(int x) {
			return x - (x & (-x));
		}

		static void build(int [] a) {
			for(int i = 0; i<a.length; i++) {
				int start = i+1;
				while(start <= n) {
					bit[start] += a[i];
					start = child(start);
				}
			}
		}

		static void update(int idx,int new_val) {
			idx += 1;
			while(idx <= n) {
				bit[idx] += new_val;
				idx = child(idx);
			}
		}

		static int query(int right) {
			int res = 0;
			while(right > 0) {
				res += bit[right];
				right = parent(right);
			}
			return res;
		}

		static int query(int left,int right) {
			return query(right) - query(left-1);
		}
	}

	public static int gcd(int a, int b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.intValue();
    }

    public static long gcd(long a, long b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.longValue();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
import java.math.BigInteger;

public class Password{
	public static FastReader fs = new FastReader();
	static final long p = 31l;
	static final long mod = 1_000_000_007;
	static char s [];
	static long hash [];
	static int n;
	static PrintWriter out = new PrintWriter(System.out);

	static boolean check(int len) {
		for(int i = 1; i+len<n; i++) {
			long hashmid = (hash[i+len-1] - hash[i-1]*power(p,len,mod))%mod;
			if(hashmid < 0)hashmid += mod;
			if(hashmid == hash[len-1])return true;
		}
		return false;
	}
	static void solve() {
		StringBuffer output = new StringBuffer();
		s = fs.next().toCharArray();
		n = s.length;
		hash = new long [n];
		hash[0] = s[0]-'a'+1;
		for(int i = 1; i<n; i++) {
			hash[i] = (hash[i-1]*p+s[i]-'a'+1)%mod;
		}
		ArrayList<Integer> matched = new ArrayList<>();
		for(int i = 1; i<=n-2; i++) {
			long prehash = hash[i-1];
			long suffhash = (hash[n-1] - (hash[n-i-1]*power(p,i,mod)))%mod;
			if(suffhash < 0)suffhash += mod;
			if(prehash == suffhash)
				matched.add(i);
		}

		//Collections.sort(matched);
		if(matched.isEmpty())
			out.println("Just a legend");
		else {
			//out.println(1);
			//for(int x : matched)
				//out.println(x);
			int start = 0,end = matched.size()-1;
			int ans = -1;
			while(start <= end) {
				int mid = start + (end - start) / 2;
				if(check(matched.get(mid))) {
					ans = matched.get(mid);
					start = mid+1;
				}
				else
					end = mid-1;
			}
			if(ans == -1) {
				out.println("Just a legend");
				return;
			}
			for(int i = 0; i<ans; i++)
				out.print(s[i]);
			out.println();
		}
	}

	public static void main(String[] args) {
		Thread t = new Thread(null,null,"",1 << 28) {
			public void run() {
				int test_case = 1;
				//test_case = fs.nextInt();
				for(int cs = 1; cs <= test_case; cs++) {
					solve();
				}
				out.close();
			}
		};
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

	static int ceil(int x,int y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static long ceil(long x,long y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static int max(int x,int y) {
		return Math.max(x, y);
	}

	static int min(int x,int y) {
		return Math.min(x, y);
	}

	static long max(long x,long y) {
		return Math.max(x, y);
	}

	static long min(long x,long y) {
		return Math.min(x, y);
	}

	static int min(int a []) {
		int x = 1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static int max(int a []) {
		int x = -1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static long min(long a []) {
		long x = (long)3e18;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static long max(long a []) {
		long x = -(long)3e18;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static int power(int x,int y) {
		int res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y) {
		long res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y,long mod) {
		long res = 1;
		x %= mod;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x) % mod;
			y >>= 1;
            x = (x * x) % mod;
		}
		return res;
	}

	static void intsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverseintsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverselongsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void intpairsort(intpair [] a) {
		List<intpair> temp = new ArrayList<intpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longpairsort(longpair [] a) {
		List<longpair> temp = new ArrayList<longpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static class intpair{
		public int x;
		public int y;

		intpair(int a,int b){
			this.x = a;
			this.y = b;
		}

		intpair(){}
	}

	static class longpair{
		public long x;
		public long y;

		longpair(long a,long b){
			this.x = a;
			this.y = b;
		}

		longpair(){}
	}

	static class data{
		public long sum;

		data(long val){
		    this.sum = val;
		}

		data(){}

		data combine(data l, data r) {
		    data res = new data();
		    res.sum = l.sum + r.sum;
		    return res;
		}
	}

	static class Seg_Tree extends data{
		public int n;
		data [] seg;

		Seg_Tree(int sz){
			this.n = sz;
			seg = new data[4*n+4];
		}

		void build(long a[], int v, int tl, int tr) {
		    if (tl == tr) {
		        seg[v] = new data(a[tl]);
		    } else {
		        int tm = (tl + tr) / 2;
		        build(a, v*2, tl, tm);
		        build(a, v*2+1, tm+1, tr);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		void update(int v, int tl, int tr, int pos, long new_val) {
		    if (tl == tr) {
		        seg[v] = new data(new_val);
		    } else {
		        int tm = (tl + tr) / 2;
		        if (pos <= tm)
		            update(v*2, tl, tm, pos, new_val);
		        else
		            update(v*2+1, tm+1, tr, pos, new_val);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		data query(int v, int tl, int tr, int l, int r) {
		    if (l > r)
		        return new data(0);
		    if (l == tl && r == tr)
		        return seg[v];
		    int tm = (tl + tr) / 2;
		    return combine(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r));
		}
	}

	static class Bit_Tree{
		static int n;
		static int [] bit;

		Bit_Tree(int sz){
			Bit_Tree.n = sz;
			Bit_Tree.bit = new int[n+1];
		}

		static int child(int x) {
			return x + (x & (-x));
		}

		static int parent(int x) {
			return x - (x & (-x));
		}

		static void build(int [] a) {
			for(int i = 0; i<a.length; i++) {
				int start = i+1;
				while(start <= n) {
					bit[start] += a[i];
					start = child(start);
				}
			}
		}

		static void update(int idx,int new_val) {
			idx += 1;
			while(idx <= n) {
				bit[idx] += new_val;
				idx = child(idx);
			}
		}

		static int query(int right) {
			int res = 0;
			while(right > 0) {
				res += bit[right];
				right = parent(right);
			}
			return res;
		}

		static int query(int left,int right) {
			return query(right) - query(left-1);
		}
	}

	public static int gcd(int a, int b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.intValue();
    }

    public static long gcd(long a, long b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.longValue();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
import java.math.BigInteger;

public class Password{
	public static FastReader fs = new FastReader();
	static final long p = 31l;
	static final long mod = 1_000_000_007;
	static char s [];
	static long hash [];
	static long powers [];
	static int n;
	static PrintWriter out = new PrintWriter(System.out);

	static boolean check(int len) {
		for(int i = 1; i+len<n; i++) {
			long hashmid = (hash[i+len-1] - hash[i-1]*powers[len])%mod;
			if(hashmid < 0)hashmid += mod;
			if(hashmid == hash[len-1])return true;
		}
		return false;
	}
	static void solve() {
		StringBuffer output = new StringBuffer();
		s = fs.next().toCharArray();
		n = s.length;
		hash = new long [n];
		powers = new long [n+1];
		hash[0] = s[0]-'a'+1;
		for(int i = 1; i<n; i++) {
			hash[i] = (hash[i-1]*p+s[i]-'a'+1)%mod;
			powers[i] = power(p,i,mod);
		}
		//powers[n] = power(p,n,mod);
		ArrayList<Integer> matched = new ArrayList<>();
		for(int i = 1; i<=n-2; i++) {
			long prehash = hash[i-1];
			long suffhash = (hash[n-1] - (hash[n-i-1]*powers[i]))%mod;
			if(suffhash < 0)suffhash += mod;
			if(prehash == suffhash)
				matched.add(i);
		}

		//Collections.sort(matched);
		if(matched.isEmpty())
			out.println("Just a legend");
		else {
			//out.println(1);
			//for(int x : matched)
				//out.println(x);
			int start = 0,end = matched.size()-1;
			int ans = -1;
			while(start <= end) {
				int mid = start + (end - start) / 2;
				if(check(matched.get(mid))) {
					ans = matched.get(mid);
					start = mid+1;
				}
				else
					end = mid-1;
			}
			if(ans == -1) {
				out.println("Just a legend");
				return;
			}
			for(int i = 0; i<ans; i++)
				out.print(s[i]);
			out.println();
		}
	}

	public static void main(String[] args) {
		Thread t = new Thread(null,null,"",1 << 28) {
			public void run() {
				int test_case = 1;
				//test_case = fs.nextInt();
				for(int cs = 1; cs <= test_case; cs++) {
					solve();
				}
				out.close();
			}
		};
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

	static int ceil(int x,int y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static long ceil(long x,long y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static int max(int x,int y) {
		return Math.max(x, y);
	}

	static int min(int x,int y) {
		return Math.min(x, y);
	}

	static long max(long x,long y) {
		return Math.max(x, y);
	}

	static long min(long x,long y) {
		return Math.min(x, y);
	}

	static int min(int a []) {
		int x = 1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static int max(int a []) {
		int x = -1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static long min(long a []) {
		long x = (long)3e18;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static long max(long a []) {
		long x = -(long)3e18;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static int power(int x,int y) {
		int res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y) {
		long res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y,long mod) {
		long res = 1;
		x %= mod;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x) % mod;
			y >>= 1;
            x = (x * x) % mod;
		}
		return res;
	}

	static void intsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverseintsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverselongsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void intpairsort(intpair [] a) {
		List<intpair> temp = new ArrayList<intpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longpairsort(longpair [] a) {
		List<longpair> temp = new ArrayList<longpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static class intpair{
		public int x;
		public int y;

		intpair(int a,int b){
			this.x = a;
			this.y = b;
		}

		intpair(){}
	}

	static class longpair{
		public long x;
		public long y;

		longpair(long a,long b){
			this.x = a;
			this.y = b;
		}

		longpair(){}
	}

	static class data{
		public long sum;

		data(long val){
		    this.sum = val;
		}

		data(){}

		data combine(data l, data r) {
		    data res = new data();
		    res.sum = l.sum + r.sum;
		    return res;
		}
	}

	static class Seg_Tree extends data{
		public int n;
		data [] seg;

		Seg_Tree(int sz){
			this.n = sz;
			seg = new data[4*n+4];
		}

		void build(long a[], int v, int tl, int tr) {
		    if (tl == tr) {
		        seg[v] = new data(a[tl]);
		    } else {
		        int tm = (tl + tr) / 2;
		        build(a, v*2, tl, tm);
		        build(a, v*2+1, tm+1, tr);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		void update(int v, int tl, int tr, int pos, long new_val) {
		    if (tl == tr) {
		        seg[v] = new data(new_val);
		    } else {
		        int tm = (tl + tr) / 2;
		        if (pos <= tm)
		            update(v*2, tl, tm, pos, new_val);
		        else
		            update(v*2+1, tm+1, tr, pos, new_val);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		data query(int v, int tl, int tr, int l, int r) {
		    if (l > r)
		        return new data(0);
		    if (l == tl && r == tr)
		        return seg[v];
		    int tm = (tl + tr) / 2;
		    return combine(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r));
		}
	}

	static class Bit_Tree{
		static int n;
		static int [] bit;

		Bit_Tree(int sz){
			Bit_Tree.n = sz;
			Bit_Tree.bit = new int[n+1];
		}

		static int child(int x) {
			return x + (x & (-x));
		}

		static int parent(int x) {
			return x - (x & (-x));
		}

		static void build(int [] a) {
			for(int i = 0; i<a.length; i++) {
				int start = i+1;
				while(start <= n) {
					bit[start] += a[i];
					start = child(start);
				}
			}
		}

		static void update(int idx,int new_val) {
			idx += 1;
			while(idx <= n) {
				bit[idx] += new_val;
				idx = child(idx);
			}
		}

		static int query(int right) {
			int res = 0;
			while(right > 0) {
				res += bit[right];
				right = parent(right);
			}
			return res;
		}

		static int query(int left,int right) {
			return query(right) - query(left-1);
		}
	}

	public static int gcd(int a, int b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.intValue();
    }

    public static long gcd(long a, long b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.longValue();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
import java.math.BigInteger;

public class Password{
	public static FastReader fs = new FastReader();
	static final long p = 31l;
	static final long mod = 1_000_000_007;
	static char s [];
	static long hash [];
	static PrintWriter out = new PrintWriter(System.out);

	interface hashing{
		boolean check(int len);
	}

	static void solve() {
		StringBuffer output = new StringBuffer();
		s = fs.next().toCharArray();
		int n = s.length;
		hash = new long [n];
		hash[0] = s[0]-'a'+1;
		for(int i = 1; i<n; i++) {
			hash[i] = (hash[i-1]*p+s[i]-'a'+1)%mod;
		}
		ArrayList<Integer> matched = new ArrayList<>();
		for(int i = 1; i<=n-2; i++) {
			long prehash = hash[i-1];
			long suffhash = (hash[n-1] - (hash[n-i-1]*power(p,i,mod)))%mod;
			if(suffhash < 0)suffhash += mod;
			if(prehash == suffhash)
				matched.add(i);
		}

		hashing hs = new hashing() {
			public boolean check(int len) {
				for(int i = 1; i+len<n; i++) {
					long hashmid = (hash[i+len-1] - hash[i-1]*power(p,len,mod))%mod;
					if(hashmid < 0)hashmid += mod;
					if(hashmid == hash[len-1])return true;
				}
				return false;
			}
		};
		//Collections.sort(matched);
		if(matched.isEmpty())
			out.println("Just a legend");
		else {
			//out.println(1);
			//for(int x : matched)
				//out.println(x);
			int start = 0,end = matched.size()-1;
			int ans = -1;
			while(start <= end) {
				int mid = start + (end - start) / 2;
				if(hs.check(matched.get(mid))) {
					ans = matched.get(mid);
					start = mid+1;
				}
				else
					end = mid-1;
			}
			if(ans == -1) {
				out.println("Just a legend");
				return;
			}
			for(int i = 0; i<ans; i++)
				out.print(s[i]);
			out.println();
		}
	}

	public static void main(String[] args) {
		Thread t = new Thread(null,null,"",1 << 28) {
			public void run() {
				int test_case = 1;
				//test_case = fs.nextInt();
				for(int cs = 1; cs <= test_case; cs++) {
					solve();
				}
				out.close();
			}
		};
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

	static int ceil(int x,int y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static long ceil(long x,long y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static int max(int x,int y) {
		return Math.max(x, y);
	}

	static int min(int x,int y) {
		return Math.min(x, y);
	}

	static long max(long x,long y) {
		return Math.max(x, y);
	}

	static long min(long x,long y) {
		return Math.min(x, y);
	}

	static int min(int a []) {
		int x = 1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static int max(int a []) {
		int x = -1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static long min(long a []) {
		long x = (long)3e18;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static long max(long a []) {
		long x = -(long)3e18;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static int power(int x,int y) {
		int res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y) {
		long res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y,long mod) {
		long res = 1;
		x %= mod;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x) % mod;
			y >>= 1;
            x = (x * x) % mod;
		}
		return res;
	}

	static void intsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverseintsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverselongsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void intpairsort(intpair [] a) {
		List<intpair> temp = new ArrayList<intpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longpairsort(longpair [] a) {
		List<longpair> temp = new ArrayList<longpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static class intpair{
		public int x;
		public int y;

		intpair(int a,int b){
			this.x = a;
			this.y = b;
		}

		intpair(){}
	}

	static class longpair{
		public long x;
		public long y;

		longpair(long a,long b){
			this.x = a;
			this.y = b;
		}

		longpair(){}
	}

	static class data{
		public long sum;

		data(long val){
		    this.sum = val;
		}

		data(){}

		data combine(data l, data r) {
		    data res = new data();
		    res.sum = l.sum + r.sum;
		    return res;
		}
	}

	static class Seg_Tree extends data{
		public int n;
		data [] seg;

		Seg_Tree(int sz){
			this.n = sz;
			seg = new data[4*n+4];
		}

		void build(long a[], int v, int tl, int tr) {
		    if (tl == tr) {
		        seg[v] = new data(a[tl]);
		    } else {
		        int tm = (tl + tr) / 2;
		        build(a, v*2, tl, tm);
		        build(a, v*2+1, tm+1, tr);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		void update(int v, int tl, int tr, int pos, long new_val) {
		    if (tl == tr) {
		        seg[v] = new data(new_val);
		    } else {
		        int tm = (tl + tr) / 2;
		        if (pos <= tm)
		            update(v*2, tl, tm, pos, new_val);
		        else
		            update(v*2+1, tm+1, tr, pos, new_val);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		data query(int v, int tl, int tr, int l, int r) {
		    if (l > r)
		        return new data(0);
		    if (l == tl && r == tr)
		        return seg[v];
		    int tm = (tl + tr) / 2;
		    return combine(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r));
		}
	}

	static class Bit_Tree{
		static int n;
		static int [] bit;

		Bit_Tree(int sz){
			Bit_Tree.n = sz;
			Bit_Tree.bit = new int[n+1];
		}

		static int child(int x) {
			return x + (x & (-x));
		}

		static int parent(int x) {
			return x - (x & (-x));
		}

		static void build(int [] a) {
			for(int i = 0; i<a.length; i++) {
				int start = i+1;
				while(start <= n) {
					bit[start] += a[i];
					start = child(start);
				}
			}
		}

		static void update(int idx,int new_val) {
			idx += 1;
			while(idx <= n) {
				bit[idx] += new_val;
				idx = child(idx);
			}
		}

		static int query(int right) {
			int res = 0;
			while(right > 0) {
				res += bit[right];
				right = parent(right);
			}
			return res;
		}

		static int query(int left,int right) {
			return query(right) - query(left-1);
		}
	}

	public static int gcd(int a, int b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.intValue();
    }

    public static long gcd(long a, long b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.longValue();
    }
}


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.*;
import java.math.BigInteger;

public class Password{
	public static FastReader fs = new FastReader();
	static final long p = 31l;
	static final long mod = 1_000_000_007;
	static char s [];
	static long hash [];
	static PrintWriter out = new PrintWriter(System.out);

	interface hashing{
		boolean check(int len);
	}

	static void solve() {
		StringBuffer output = new StringBuffer();
		s = fs.next().toCharArray();
		int n = s.length;
		hash = new long [n];
		hash[0] = s[0]-'a'+1;
		for(int i = 1; i<n; i++) {
			hash[i] = (hash[i-1]*p+s[i]-'a'+1)%mod;
		}
		ArrayList<Integer> matched = new ArrayList<>();
		for(int i = 1; i<=n-2; i++) {
			long prehash = hash[i-1];
			long suffhash = (hash[n-1] - (hash[n-i-1]*power(p,i,mod))%mod+mod)%mod;
			if(prehash == suffhash)
				matched.add(i);
		}

		hashing hs = new hashing() {
			public boolean check(int len) {
				for(int i = 1; i+len<n; i++) {
					long hashmid = (hash[i+len-1] - (hash[i-1]*power(p,len,mod))%mod+mod)%mod;
					if(hashmid == hash[len-1])return true;
				}
				return false;
			}
		};
		//Collections.sort(matched);
		if(matched.isEmpty())
			out.println("Just a legend");
		else {
			//out.println(1);
			//for(int x : matched)
				//out.println(x);
			int start = 0,end = matched.size()-1;
			int ans = -1;
			while(start <= end) {
				int mid = start + (end - start) / 2;
				if(hs.check(matched.get(mid))) {
					ans = matched.get(mid);
					start = mid+1;
				}
				else
					end = mid-1;
			}
			if(ans == -1) {
				out.println("Just a legend");
				return;
			}
			for(int i = 0; i<ans; i++)
				out.print(s[i]);
			out.println();
		}
	}

	public static void main(String[] args) {
		Thread t = new Thread(null,null,"",1 << 28) {
			public void run() {
				int test_case = 1;
				//test_case = fs.nextInt();
				for(int cs = 1; cs <= test_case; cs++) {
					solve();
				}
				out.close();
			}
		};
		t.start();
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

	static int ceil(int x,int y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static long ceil(long x,long y) {
		return (x % y == 0 ? x / y : (x / y +1));
	}

	static int max(int x,int y) {
		return Math.max(x, y);
	}

	static int min(int x,int y) {
		return Math.min(x, y);
	}

	static long max(long x,long y) {
		return Math.max(x, y);
	}

	static long min(long x,long y) {
		return Math.min(x, y);
	}

	static int min(int a []) {
		int x = 1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static int max(int a []) {
		int x = -1_000_000_00_9;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static long min(long a []) {
		long x = (long)3e18;
		for(int i = 0; i<a.length; i++)x = min(x,a[i]);
		return x;
	}

	static long max(long a []) {
		long x = -(long)3e18;
		for(int i = 0; i<a.length; i++)x = max(x,a[i]);
		return x;
	}

	static int power(int x,int y) {
		int res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y) {
		long res = 1;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x);
			y >>= 1;
            x = (x * x);
		}
		return res;
	}

	static long power(long x,long y,long mod) {
		long res = 1;
		x %= mod;
		while(y > 0) {
			if( y % 2 == 1)res = (res * x) % mod;
			y >>= 1;
            x = (x * x) % mod;
		}
		return res;
	}

	static void intsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverseintsort(int [] a) {
		List<Integer> temp = new ArrayList<Integer>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void reverselongsort(long [] a) {
		List<Long> temp = new ArrayList<Long>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp);
		Collections.reverseOrder();
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void intpairsort(intpair [] a) {
		List<intpair> temp = new ArrayList<intpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static void longpairsort(longpair [] a) {
		List<longpair> temp = new ArrayList<longpair>();
		for(int i = 0; i<a.length; i++)temp.add(a[i]);
		Collections.sort(temp,(p1,p2) -> {
			if(p1.x == p2.x) return p1.y >= p2.y ? -1 : 1;
			else return p1.x > p2.x ? -1 : 1;
		});
		for(int i = 0; i<a.length; i++)a[i] = temp.get(i);
	}

	static class intpair{
		public int x;
		public int y;

		intpair(int a,int b){
			this.x = a;
			this.y = b;
		}

		intpair(){}
	}

	static class longpair{
		public long x;
		public long y;

		longpair(long a,long b){
			this.x = a;
			this.y = b;
		}

		longpair(){}
	}

	static class data{
		public long sum;

		data(long val){
		    this.sum = val;
		}

		data(){}

		data combine(data l, data r) {
		    data res = new data();
		    res.sum = l.sum + r.sum;
		    return res;
		}
	}

	static class Seg_Tree extends data{
		public int n;
		data [] seg;

		Seg_Tree(int sz){
			this.n = sz;
			seg = new data[4*n+4];
		}

		void build(long a[], int v, int tl, int tr) {
		    if (tl == tr) {
		        seg[v] = new data(a[tl]);
		    } else {
		        int tm = (tl + tr) / 2;
		        build(a, v*2, tl, tm);
		        build(a, v*2+1, tm+1, tr);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		void update(int v, int tl, int tr, int pos, long new_val) {
		    if (tl == tr) {
		        seg[v] = new data(new_val);
		    } else {
		        int tm = (tl + tr) / 2;
		        if (pos <= tm)
		            update(v*2, tl, tm, pos, new_val);
		        else
		            update(v*2+1, tm+1, tr, pos, new_val);
		        seg[v] = combine(seg[v*2],seg[v*2+1]);
		    }
		}

		data query(int v, int tl, int tr, int l, int r) {
		    if (l > r)
		        return new data(0);
		    if (l == tl && r == tr)
		        return seg[v];
		    int tm = (tl + tr) / 2;
		    return combine(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r));
		}
	}

	static class Bit_Tree{
		static int n;
		static int [] bit;

		Bit_Tree(int sz){
			Bit_Tree.n = sz;
			Bit_Tree.bit = new int[n+1];
		}

		static int child(int x) {
			return x + (x & (-x));
		}

		static int parent(int x) {
			return x - (x & (-x));
		}

		static void build(int [] a) {
			for(int i = 0; i<a.length; i++) {
				int start = i+1;
				while(start <= n) {
					bit[start] += a[i];
					start = child(start);
				}
			}
		}

		static void update(int idx,int new_val) {
			idx += 1;
			while(idx <= n) {
				bit[idx] += new_val;
				idx = child(idx);
			}
		}

		static int query(int right) {
			int res = 0;
			while(right > 0) {
				res += bit[right];
				right = parent(right);
			}
			return res;
		}

		static int query(int left,int right) {
			return query(right) - query(left-1);
		}
	}

	public static int gcd(int a, int b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.intValue();
    }

    public static long gcd(long a, long b)
    {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
            return gcd.longValue();
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        char[] input = scan.nextLine().toCharArray();

        int length = input.length;
        int[] next = new int[length];
        next[0] = -1;
        int k = -1;
        int j = 0;
        while (j < length-1){
            if(k==-1 || input[k]==input[j]){
                k++;
                j++;
                next[j] = k;
            }
            else {
                k = next[k];
            }
        }
        int[] visit = new int[length];
        next[0] = 0;
        for (int h = 0; h < length-1; h++){
            visit[next[h]] = 1;
        }
        int last = length-1;

        while(next[last]!=0){
            if(visit[next[last]]==1){
                System.out.println(Arrays.copyOfRange(input,0,next[last]+1));
                return;
            }
            last = next[last];
        }
        System.out.println("Just a legend");
    }
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String input1= sc.next();
        input1 += "#";
        char[] input = input1.toCharArray();
        int length = input.length;
        int[] next = new int[length];
        next[0] = -1;
        next[1] = 0;
        int i = 2;
        int cn = 0;
        while (i < length) {
            if(input[i-1] == input[cn]){
                next[i++] = ++cn;
            } else if (cn > 0) {
                cn = next[cn];
            } else {
                next[i++] = 0;
            }
        }
        int[] visit = new int[length];
        next[0] = 0;
        for (int h = 0; h < length-1; h++){
            visit[next[h]] = 1;
        }
        int last = length-1;

        while(next[last]!=0){
            if(visit[next[last]]==1){
                System.out.println(Arrays.copyOfRange(input,0,next[last]));
                return;
            }
            last = next[last];
        }
        System.out.println("Just a legend");

    }
}


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void nextOpe(char[]str){
        int[] next = new int[str.length];
        next[0] = -1;
        next[1] = 0;
        int i = 2;
        int cn = 0;
        while (i < str.length) {
            if(str[i-1] == str[cn]){
                next[i++] = ++cn;
            } else if (cn > 0) {
                cn = next[cn];
            } else {
                next[i++] = 0;
            }
        }
        int[] vis = new int[str.length];
        next[0] = 0;
        for(int h = 0; h < str.length-1; h++){
            vis[next[h]] = 1;
        }
        i = str.length - 1;
        boolean flag = false;
        while(next[i] != 0){
            if(vis[next[i]] == 1){
                System.out.println(Arrays.copyOfRange(str,0,next[i]));
                flag = true;
                break;
            }
            i = next[i];
        }
        if(!flag)
            System.out.println("Just a legend");
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        str += "#";
        nextOpe(str.toCharArray());
    }
}


import java.util.Scanner;

public class Main {

    public static int[] getNext(String str){
        str = str + "#";
        char[] str_arr = str.toCharArray();
        int[] next = new int[str_arr.length];
        next[0] = -1;
        next[1] = 0;
        int i = 2;
        int cn = 0;
        while(i < str_arr.length){
            if(str_arr[i-1] == str_arr[cn]){
                next[i++] = ++cn;
            }else if(cn > 0){
                cn = next[cn];
            }else{
                next[i++] = 0;
            }
        }
        return next;
    }

    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         String str = sc.next();
         int[] next = getNext(str);
         int len = str.length();
         int[] hasMidLen = new int[len];
         next[0] = 0;
         for(int i = 0; i < len; ++i){
             hasMidLen[next[i]] = 1;
         }
         int j = len;
         boolean flag = false;
         while(next[j] != 0){
             if(hasMidLen[next[j]] == 1){
                 System.out.println(str.substring(0, next[j]));
                 flag = true;
                 break;
             }
             j = next[j];
         }
         if(!flag){
             System.out.println("Just a legend");
         }
    }

}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        char[] input = scan.nextLine().toCharArray();
        int length = input.length;
        int[] next = new int[length];
        next[0] = -1;
        int k = -1;
        int j = 0;
        while (j < length-1){
            if(k==-1 || input[k]==input[j]){
                k++;
                j++;
                next[j] = k;
            }
            else {
                k = next[k];
            }
        }
        int[] visit = new int[length];
        next[0] = 0;
        for (int h = 0; h < length-1; h++){
            visit[next[h]] = 1;
        }
        int last = length-1;

        while(next[last]!=0){
            if(visit[next[last]]==1){
                System.out.println(Arrays.copyOfRange(input,0,next[last]+1));
                return;
            }
            last = next[last];
        }
        System.out.println("Just a legend");

    }
}


import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String input1= sc.next();
        input1 += "#";
        char[] input = input1.toCharArray();
        int length = input.length;
        int[] next = new int[length];
        next[0] = -1;
        next[1] = 0;
        int i = 2;
        int cn = 0;
        while (i < length) {
            if(input[i-1] == input[cn]){
                next[i++] = ++cn;
            } else if (cn > 0) {
                cn = next[cn];
            } else {
                next[i++] = 0;
            }
        }
        int[] visit = new int[length];
        next[0] = 0;
        for (int h = 0; h < length-1; h++){
            visit[next[h]] = 1;
        }
        int last = length-1;

        while(next[last]!=0){
            if(visit[next[last]]==1){
                System.out.println(Arrays.copyOfRange(input,0,next[last]));
                return;
            }
            last = next[last];
        }
        System.out.println("Just a legend");

    }
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        char[] input = scan.nextLine().toCharArray();
        int length = input.length;
        int[] next = new int[length];
        next[0] = -1;
        int k = -1;
        int j = 0;
        while (j < length){
            if(k==-1 || input[k]==input[j]){
                k++;
                j++;
                next[j] = k;
            }
            else {
                k = next[k];
            }
        }
        int[] visit = new int[length];
        next[0] = 0;
        for (int h = 0; h < length-1; h++){
            visit[next[h]] = 1;
        }
        int last = length-1;

        while(next[last]!=0){
            if(visit[next[last]]==1){
                System.out.println(Arrays.copyOfRange(input,0,next[last]+1));
                return;
            }
            last = next[last];
        }
        System.out.println("Just a legend");

    }
}


import java.util.Scanner;

public class Main {
    public static void nextOpe(char[]str){
        int[] next = new int[str.length];
        next[0] = -1;
        next[1] = 0;
        int i = 2;
        int cn = 0;
        while (i < str.length) {
            if(str[i-1] == str[cn]){
                next[i++] = ++cn;
            } else if (cn > 0) {
                cn = next[cn];
            } else {
                next[i++] = 0;
            }
        }
        int[] vis = new int[str.length];
        next[0] = 0;
        for(int h = 0; h < str.length-1; h++){
            vis[next[h]] = 1;
        }
        i = str.length - 1;
        boolean flag = false;
        while(next[i] != 0){
            if(vis[next[i]] == 1){
                System.out.println(Arrays.copyOfRange(str,0,next[i]));
                flag = true;
                break;
            }
            i = next[i];
        }
        if(!flag)
            System.out.println("Just a legend");
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        str += "#";
        nextOpe(str.toCharArray());
    }
}


#include <iostream>
#include <cstdio>
#include <cstdlib>

/* D. Friends and Subsequences */

using namespace std;
typedef long long ll;

#define MAXN 200005

int dpmax[MAXN][20], dpmin[MAXN][20];
int a[MAXN], b[MAXN];
int n;

void initRMQ()
{
	for (int i = 0; i < n; i ++)
	{
		dpmax[i][0] = a[i];
		dpmin[i][0] = b[i];
	}
	for (int j = 1; (1 << j) <= n; j ++)
	{
		for (int i = 0; i + (1 << j) - 1 < n; i ++)
		{
			dpmin[i][j] = min(dpmin[i][j - 1], dpmin[i + (1 << (j - 1))][j - 1]);
			dpmax[i][j] = max(dpmax[i][j - 1], dpmax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int rmq_max(int l, int r)
{
	int k = 0;
	while((1 << (k + 1)) <= r - l + 1)
		k ++;
	return max(dpmax[l][k], dpmax[r - (1 << k) + 1][k]);
}

int rmq_min(int l, int r)
{
	int k = 0;
	while((1 << (k + 1)) <= r - l + 1)
		k ++;
	return min(dpmin[l][k], dpmin[r - (1 << k) + 1][k]);
}

int main(int argc, char * argv[])
{
	scanf("%d", &n);
	for (int i = 0; i < n; i ++)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; i ++)
		scanf("%d", &b[i]);
	initRMQ();

	int l, r, ma, mb, mid, lr, rr;
	int sum = 0;

	for (int i = 0; i < n; i ++)
	{
		l = i;
		r = n - 1;
		lr = -1;
		while(l <= r)
		{
			mid = (l + r) / 2;
			ma = rmq_max(i, mid);
			mb = rmq_min(i, mid);
			if (ma >= mb)
			{
				if (ma == mb)
					lr = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}
		if (lr == -1)
			continue;
		l = i;
		r = n - 1;
		rr = -1;
		while(l <= r)
		{
			mid = (l + r) / 2;
			ma = rmq_max(i, mid);
			mb = rmq_min(i, mid);
			if (ma > mb)
				r = mid - 1;
			else
			{
				if (ma == mb)
				{
					rr = mid;
				}
				l = mid + 1;
			}
		}
		sum += (rr - lr + 1);
	}
	printf("%d\n", sum);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>

/* D. Friends and Subsequences */

using namespace std;
typedef long long ll;

#define MAXN 200005

int dpmax[MAXN][20], dpmin[MAXN][20];
int a[MAXN], b[MAXN];
int n;

void initRMQ()
{
	for (int i = 0; i < n; i ++)
	{
		dpmax[i][0] = a[i];
		dpmin[i][0] = b[i];
	}
	for (int j = 1; (1 << j) <= n; j ++)
	{
		for (int i = 0; i + (1 << j) - 1 < n; i ++)
		{
			dpmin[i][j] = min(dpmin[i][j - 1], dpmin[i + (1 << (j - 1))][j - 1]);
			dpmax[i][j] = max(dpmax[i][j - 1], dpmax[i + (1 << (j - 1))][j - 1]);
		}
	}
}

int rmq_max(int l, int r)
{
	int k = 0;
	while((1 << (k + 1)) <= r - l + 1)
		k ++;
	return max(dpmax[l][k], dpmax[r - (1 << k) + 1][k]);
}

int rmq_min(int l, int r)
{
	int k = 0;
	while((1 << (k + 1)) <= r - l + 1)
		k ++;
	return min(dpmin[l][k], dpmin[r - (1 << k) + 1][k]);
}

int main(int argc, char * argv[])
{
	scanf("%d", &n);
	for (int i = 0; i < n; i ++)
		scanf("%d", &a[i]);
	for (int i = 0; i < n; i ++)
		scanf("%d", &b[i]);
	initRMQ();

	int l, r, ma, mb, mid, lr, rr;
	int sum = 0;

	for (int i = 0; i < n; i ++)
	{
		l = i;
		r = n - 1;
		lr = -1;
		while(l <= r)
		{
			mid = (l + r) / 2;
			ma = rmq_max(l, mid);
			mb = rmq_min(l, mid);
			if (ma >= mb)
			{
				if (ma == mb)
					lr = mid;
				r = mid - 1;
			}
			else
				l = mid + 1;
		}
		if (lr == -1)
			continue;
		l = i;
		r = n - 1;
		rr = -1;
		while(l <= r)
		{
			mid = (l + r) / 2;
			ma = rmq_max(l, mid);
			mb = rmq_min(l, mid);
			if (ma > mb)
				r = mid - 1;
			else
			{
				if (ma == mb)
				{
					rr = mid;
				}
				l = mid + 1;
			}
		}
		sum += (rr - lr + 1);
	}
	printf("%d\n", sum);
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 1000005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

int n;
long long a[MAX],b[MAX],maxn[MAX][20],minn[MAX][20];

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1;i<=n;i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i+(1<<(j-1))<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    int Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    return (Maxn==Minn);
}

int main()
{
    long long ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                if(RMQ(i,j))
                    ans++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][30],minn[MAX][30],n;

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1; i<=n; i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<(j))-1<=n; i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(int l,int r)
{
    long long k=0;
    long long Maxn,Minn;
    k=(log(r-l))/log(2);
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn > Minn)
        return 1;
    else if(Maxn < Minn)
        return -1;
    return 0;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

int n,a[MAX],b[MAX],maxn[MAX][20],minn[MAX][20];

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1;i<=n;i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1;j<18;j++)
    {
        for(int i=1;i<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    int Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    return (Maxn==Minn);
}

int main()
{
    int ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        MakeTab();
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                if(RMQ(i,j))
                    ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][30],minn[MAX][30],n;

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1; i<=n; i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<(j))-1<=n; i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(int l,int r)
{
    long long k=0;
    long long Maxn,Minn;
    k=(log(r-l+1))/log(2);
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn > Minn)
        return 1;
    else if(Maxn < Minn)
        return -1;
    return 0;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }

    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][30],minn[MAX][30],n;

void MakeTab()
{
    memset(maxn,0,sizeof(maxn));
    memset(minn,0,sizeof(minn));
    for(int i=1;i<=n;i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(long long l,long long r)
{
    long long k=log(r-l+1)/log(2);
    long long Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    long long Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn==Minn)
        return 0;
    else if(Maxn > Minn)
        return 1;
    return -1;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 1000005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

int n;
long long a[MAX],b[MAX],maxn[MAX][20],minn[MAX][20];

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1;i<=n;i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i+(1<<(j))<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    int Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    return (Maxn==Minn);
}

int main()
{
    long long ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                if(RMQ(i,j))
                    ans++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 1000005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

int n;
long long a[MAX],b[MAX],maxn[MAX][20],minn[MAX][20];

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1;i<=n;i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1;j<20;j++)
    {
        for(int i=1;i<=n;i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    int Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    return (Maxn==Minn);
}

int main()
{
    long long ans;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                if(RMQ(i,j))
                    ans++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][30],minn[MAX][30],n;

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1; i<=n; i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<(j))-1<=n; i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    long long Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn > Minn)
        return 1;
    else if(Maxn < Minn)
        return -1;
    return 0;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][30],minn[MAX][30],n;

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1; i<=n; i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<(j))-1<=n; i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(int l,int r)
{
    long long k=0;
    long long Maxn,Minn;
    k=(log(r-l+1))/log(2);
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn > Minn)
        return 1;
    else if(Maxn < Minn)
        return -1;
    return 0;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][30],minn[MAX][30],n;

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1; i<=n; i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<(j))-1<=n; i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}

int RMQ(int l,int r)
{
    long long k=0;
    long long Maxn,Minn;
    while((1<<(k+1))<=(r-l+1))k++;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn > Minn)
        return 1;
    else if(Maxn < Minn)
        return -1;
    return 0;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0xffffffff
#define MAX 200005
#define Temp 1000000000
#define MOD 1000000007

using namespace std;

long long a[MAX],b[MAX],maxn[MAX][20],minn[MAX][20],n;

void MakeTab()
{
    memset(minn,0,sizeof(minn));
    memset(maxn,0,sizeof(maxn));
    for(int i=1; i<=n; i++)
    {
        maxn[i][0]=a[i];
        minn[i][0]=b[i];
    }
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<(j))-1<=n; i++)
        {
            maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j]);
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j]);
        }
    }
}

int RMQ(int l,int r)
{
    int k=(log(r-l))/log(2);
    long long Maxn,Minn;
    Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
    Minn=min(minn[l][k],minn[r-(1<<k)+1][k]);
    if(Maxn > Minn)
        return 1;
    else if(Maxn < Minn)
        return -1;
    return 0;
}

int main()
{
    long long ans,l,r,mid,pos1,pos2;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=0;
        for(int i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(int i=1; i<=n; i++)
            scanf("%lld",&b[i]);
        MakeTab();
        for(int i=1; i<=n; i++)
        {
            l=i;
            r=n;
            pos1=-1;
            while(l <= r)
            {
                mid=(l+r)/2;
                int op=RMQ(i,mid);
                if(op==0)
                {
                    pos1=mid;
                    l=mid+1;
                }
                else if(op==1)
                {
                    r=mid-1;
                }
                else if(op==-1)
                {
                    l=mid+1;
                }
            }
            if(pos1!=-1)
            {
                l=i;
                r=n;
                while(l <= r)
                {
                    mid=(l+r)/2;
                    int op=RMQ(i,mid);
                    if(op==0)
                    {
                        pos2=mid;
                        r=mid-1;
                    }
                    else if(op==1)
                    {
                        r=mid-1;
                    }
                    else if(op==-1)
                    {
                        l=mid+1;
                    }
                }
                ans+=(pos1-pos2+1);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}