using System;
using System.Collections.Generic;
using System.Linq;

namespace olimp
{
    class Program
    {
        static void Main(string[] args)
        {
            long f1 = 0, f2 = 0;
            int n = int.Parse(Console.ReadLine());
            long[] a = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
            long[] b = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
            for (int i = 0; i < n; i++)
            {
                f1 = f1 | a[i];
                f2 = f2 | b[i];
            }
            f1 += f2;
            Console.Write(f1.ToString());

        }

    }

}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlakeTech
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] a = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            int[] b = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            int[][] anda = new int[n][];
            int[][] andb = new int[n][];
            for(int i = 0; i< n; ++i)
            {
                anda[i] = new int[n];
                andb[i] = new int[n];
                anda[i][i] = a[i];
                andb[i][i] = b[i];
            }
            for(int l = 0; l < n-1; ++l)
            {
                for(int r = l+1; r < n; ++r)
                {
                    anda[l][r] = anda[l][r - 1] | a[r];
                    andb[l][r] = andb[l][r - 1] | b[r];
                }
            }
            int maxanswer = 0;
            for(int l = 0; l < n; ++l)
                for(int r = l; r < n; ++r)
                    if (anda[l][r] + andb[l][r] > maxanswer)
                        maxanswer = anda[l][r] + andb[l][r];
            Console.WriteLine(maxanswer.ToString());
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Codeforces
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            var s = Console.ReadLine().Split(' ');
            var t = Console.ReadLine().Split(' ');

            long a = 0;
            long b = 0;

            for (int i = 0; i < s.Length; i++)
            {
                a |= long.Parse(s[i]);
                b |= long.Parse(t[i]);
            }

            Console.WriteLine(a + b);
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlgoTraining.Codeforces.A_s
{

    class FastScanner : StreamReader
    {
        private string[] _line;
        private int _iterator;

        public FastScanner(Stream stream) : base(stream)
        {
            _line = null;
            _iterator = 0;
        }
        public int NextInt()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt32(_line[_iterator++]);
        }
        public long NextLong()
        {
            if (_line == null || _iterator >= _line.Length)
            {
                _line = base.ReadLine().Split(' ');
                _iterator = 0;
            }
            if (_line.Count() == 0) throw new IndexOutOfRangeException("Input string is empty");
            return Convert.ToInt64(_line[_iterator++]);
        }
    }
    class Interview344
    {
        public static void Main(string[] args)
        {
            using (FastScanner fs = new FastScanner(new BufferedStream(Console.OpenStandardInput())))
            using (StreamWriter writer = new StreamWriter(new BufferedStream(Console.OpenStandardOutput())))
            {
                int n = fs.NextInt();
                long[] a = new long[n], b = new long[n];
                for (int i = 0; i < n; i++)
                {
                    a[i] = fs.NextInt();
                }
                for (int i = 0; i < n; i++)
                {
                    b[i] = fs.NextInt();
                }
                long maxVal = a[0] + b[0];
                for (int i = 0; i < n; i++)
                {
                    long aor = a[i], bor = b[i];
                    maxVal = Math.Max(aor + bor, maxVal);
                    for (int j = i + 1; j < n; j++)
                    {
                        aor |= a[j];
                        bor |= b[j];
                        maxVal = Math.Max(aor + bor, maxVal);
                    }
                }
                writer.WriteLine(maxVal);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;

namespace prA {
    class Program {
#if ONLINE_JUDGE
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024 * 10), System.Text.Encoding.ASCII, false, 1024 * 10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), System.Text.Encoding.ASCII, 1024 * 10);
#else
        private static readonly StreamWriter writer = new StreamWriter(@"..\..\output");
        private static readonly StreamReader reader = new StreamReader(@"..\..\input");
#endif

        static void Main(string[] args) {
            int n = Next();
            long a = 0;
            long b = 0;
            for (int i = 0; i < n; i++) {
                a = a | (long) Next();
            }
            for(int i = 0; i < n; i++) {
                b = b | (long)Next();
            }
            writer.Write(a+b);

            writer.Flush();
#if !ONLINE_JUDGE
            writer.Close();
#endif
        }
        private static int Next() {
            int c;
            int res = 0;
            do {
                c = reader.Read();
                if(c == -1)
                    return res;
            } while(c != '-' && (c < '0' || c > '9'));
            int sign = 1;
            if(c == '-') {
                sign = -1;
                c = reader.Read();
            }
            res = c - '0';
            while(true) {
                c = reader.Read();
                if(c < '0' || c > '9')
                    return res * sign;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;

namespace interview
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string str1 = Console.ReadLine();
            string str2 = Console.ReadLine();
            long res1 = Convert.ToInt64(str1.Split()[1]), res2 = Convert.ToInt64(str2.Split()[1]);

            for (int i = 1; i < n; i++)
            {
                res1 = res1 | Convert.ToInt64(str1.Split()[i]);
                res2 = res2 | Convert.ToInt64(str2.Split()[i]);
            }
            Console.WriteLine(res1+res2);
        }
    }
}


using System;

namespace interview
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string str1 = Console.ReadLine();
            string str2 = Console.ReadLine();
            long res1 = Convert.ToInt64(str1.Split()[0]), res2 = Convert.ToInt64(str2.Split()[0]);

            for (int i = 1; i < n; i++)
            {
                res1 = res1 | Convert.ToInt64(str1.Split()[i]);
                res2 = res2 | Convert.ToInt64(str2.Split()[i]);
            }
            Console.WriteLine(res1+res2);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        ReadInt();
        Write(ReadIntArray().Aggregate((u, v) => u | v) + ReadIntArray().Aggregate((u, v) => u | v));
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            UInt32 n = UInt32.Parse(Console.ReadLine());
            UInt32[] a = new UInt32[n];
            UInt32[] b = new UInt32[n];

            string[] s = Console.ReadLine().Split();
            for (int i = 0; i < n; i++)
            {
                a[i] = UInt32.Parse(s[i]);
            }
            s = Console.ReadLine().Split();
            for (int i = 0; i < n; i++)
            {
                b[i] = UInt32.Parse(s[i]);
            }

            UInt32 max = UInt32.MinValue;
            for (int i = 0; i < n; i++)
            {

                UInt32 c = 0;
                UInt32 d = 0;
                for (int j = i; j < n; j++)
                {
                    c |= (a[i] | a[j]);
                    d |= (b[i] | b[j]);
                    if (c + d > max)
                        max = c + d;
                }
            }
            Console.WriteLine(max);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            UInt32 n = UInt32.Parse(Console.ReadLine());
            UInt32[] a = new UInt32[n];
            UInt32[] b = new UInt32[n];

            string[] s = Console.ReadLine().Split();
            for (int i = 0; i < n; i++)
            {
                a[i] = UInt32.Parse(s[i]);
            }
            s = Console.ReadLine().Split();
            for (int i = 0; i < n; i++)
            {
                b[i] = UInt32.Parse(s[i]);
            }

            UInt32 max = UInt32.MinValue;
            for (int i = 0; i < n; i++)
            {

                UInt32 c = 0;
                UInt32 d = 0;
                for (int j = i+1; j < n; j++)
                {
                    c |= (a[i] | a[j]);
                    d |= (b[i] | b[j]);
                    if (c + d > max)
                        max = c + d;
                }
            }
            Console.WriteLine(max);

        }
    }
}


using System;
using System.Linq;

namespace _631A_Собеседование
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.ReadLine();
			var a = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
			var b = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
			int maxA = 0, maxB = 0;
			for (var i = 0; i < a.Length; i++)
			{
				var ta = a[i];
				var tb = b[i];
				if (ta > maxA)
					maxA = ta;
				if (tb > maxB)
					maxB = tb;
				for (var j = i; j < a.Length; j++)
				{
					ta = ta | a[j];
					tb = tb | b[j];
					if (ta > maxA)
						maxA = ta;
					if (tb > maxB)
						maxB = tb;
				}

			}
			Console.WriteLine(maxA + maxB);
		}
	}
}


using System;
using System.Linq;

namespace _631A_Собеседование
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.ReadLine();
			var a = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
			var b = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
			int maxA = 0, maxB = 0;
			for (var i = 1; i < a.Length; i++)
			{
				var ta = a[i];
				var tb = b[i];
				if (ta > maxA)
					maxA = ta;
				if (tb > maxB)
					maxB = tb;
				for (var j = i; j < a.Length; j++)
				{
					ta = ta | a[j];
					tb = tb | b[j];
					if (ta > maxA)
						maxA = ta;
					if (tb > maxB)
						maxB = tb;
				}

			}
			Console.WriteLine(maxA + maxB);
		}
	}
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace _629_B
{

    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var b = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            int ora = 0, orb = 0;

            for (int i = 0; i < n; i++)
            {
                ora = ora | a[i];
                orb = orb | b[i];
            }

            Console.WriteLine((long)ora + (long)orb);
        }
    }

}


using System;
using System.Collections.Generic;
using System.Linq;

namespace codeforces
{
	class Program
	{
		public class User
		{
			public int c;

			public int r;
		}

		public class Point
		{
			public int x;

			public int y;
		}

		public class Comparer : IComparer<User>
		{
			public int Compare(User x, User y)
			{
				if (x.c < y.c || (x.c == y.c && x.r < y.r))
					return -1;
				return 1;
			}
		}

		public static long getGcd(long a, long b)
		{
			if (b == 0)
				return a;
			return getGcd(b, a % b);
		}

		public static void WriteYESNO(bool x, string yes = "YES", string no = "NO")
		{
			if (x)
				Console.WriteLine(yes);
			else
			{
				Console.WriteLine(no);
			}
		}

		public static string getString()
		{
			return Console.ReadLine();
		}

		public static List<int> getList()
		{
			var s = Console.ReadLine();
			return s.Split(' ').Select(int.Parse).ToList();
		}

		public static List<long> getLongList()
		{
			var s = Console.ReadLine();
			return s.Split(' ').Select(long.Parse).ToList();
		}

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

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

		public static int Abs(int x)
		{
			return Math.Abs(x);
		}

		public static int getInt()
		{
			var s = Console.ReadLine();
			return int.Parse(s);
		}

		public static long getLong()
		{
			var s = Console.ReadLine();
			return long.Parse(s);
		}

		public static bool Equal(int x, int y, int x1, int y1)
		{
			return x == x1 && y == y1;
		}

		public class Vertex
		{
			public List<int> edges;

			public int ct;

			public Vertex()
			{
				ct = 0;
				edges = new List<int>();
				for (var i = 0; i < 26; ++i)
					edges.Add(0);
			}
		}

		private static void Main(string[] args)
		{
/*			var input = new StreamReader("input.txt");
			var output = new StreamWriter("output.txt");*/
			var n = getInt();
			var a = getList();
			var b = getList();
			var ct = 0;
			var ct1 = 0;
			for (var i = 0; i < n; ++i)
			{
				ct = ct | a[i];
				ct1 = ct1 | b[i];
			}
			Console.WriteLine(ct + ct1);
		}
	}
}


using System;
using System.Linq;

namespace _631A
{
    class Program
    {
        static void Main()
        {
            var n = int.Parse(Console.ReadLine());
            var s1 = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var s2 = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var max = 0;
            for (int i = 0; i < n; i ++)
            {
                var sum1 = 0;
                var sum2 = 0;
                for (int j = i; j < n; j++)
                {
                    sum1 |= s1[j];
                    sum2 |= s2[j];
                    max = Math.Max(sum1 + sum2, max);
                }
            }
            Console.WriteLine(max);

        }
    }
}


using System;
namespace RectangleApplication
{
    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            var v = Console.ReadLine().Split(' ');
            var t = Console.ReadLine().Split(' ');

            int a = 0,b = 0;

            for (int i = 0; i < v.Length; i++)
            {
                a |= int.Parse(v[i]);
                b |= int.Parse(t[i]);
            }
            Console.WriteLine(a + b);
            Console.ReadLine();
        }
    }
}


using System;

namespace Task
{
    class Program
    {
        static Func<string, int> C = x => Convert.ToInt32(x);
        static Func<string> G = () => Console.ReadLine();
        static void Main(string[] args)
        {
            int n = C(G());
            string[] buf = G().Split(' ');
            int[] a = new int[n];
            int or1 = 0;
            for (int i = 0; i < n; ++i)
            {
                a[i] = C(buf[i]);
                or1 |= a[i];
            }
            buf = G().Split(' ');
            int[] b = new int[n];
            int or2 = 0;
            for (int i = 0; i < n; ++i)
            {
                b[i] = C(buf[i]);
                or2 |= b[i];
            }
            Console.WriteLine(or1 + or2);
        }
    }
}


using System;

namespace Task
{
    class Program
    {
        static Func<string, int> C = x => Convert.ToInt32(x);
        static Func<string> G = () => Console.ReadLine();
        static void Main(string[] args)
        {
            int n = C(G());
            string[] buf = G().Split(' ');
            int[] a = new int[n];
            int xor1 = 0;
            for (int i = 0; i < n; ++i)
            {
                a[i] = C(buf[i]);
                xor1 ^= a[i];
            }
            buf = G().Split(' ');
            int[] b = new int[n];
            int xor2 = 0;
            for (int i = 0; i < n; ++i)
            {
                b[i] = C(buf[i]);
                xor2 ^= b[i];
            }
            int xora, xorb;
            for (int i = 0; i < n; ++i)
            {
                for (int j = i; j < n; ++j)
                {
                    xora = 0;
                    xorb = 0;
                    for (int k = i; k <= j; ++k)
                    {
                        xora |= a[k];
                        xorb |= b[k];
                    }
                    if (xora + xorb > xor1 + xor2)
                    {

                        xor1 = xora;
                        xor2 = xorb;
                    }
                }
            }
            Console.WriteLine(xor1 + xor2);
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Threading;

class Program
{
    public static long ScalarProduct(long x1, long y1, long x2, long y2)
    {
        return x1 * y2 - x2 * y1;
    }

    private static void Main(string[] args)
    {
        var n = ReadInt();
        var a = ReadIntArray(n);
        var b = ReadIntArray(n);

        int ax = 0;
        int bx = 0;
        for (int i = 0; i < n; i++) ax |= a[i];
        for (int i = 0; i < n; i++) bx |= b[i];

        Console.WriteLine(ax + bx);
    }

    private static Random rnd = new Random();
    private static List<int>[] GetRandomGraph(int n, int[] aa)
    {
        List<int>[] g = new List<int>[n];
        for (int i = 0; i < n; i++) g[i] = new List<int>();
        for (int i = 1; i < n; i++)
        {
            int a = i;
            int b = rnd.Next(i);

            g[a].Add(b);
            g[b].Add(a);
        }

        MarkNodes(1, -1, ref n, aa, g);

        for (int i = 0; i < g.Length; i++)
        {
            for (int j = 1; j < g[i].Count; j++)
            {
                int x = rnd.Next(j);

                int tmp = g[i][x];
                g[i][x] = g[i][j];
                g[i][j] = tmp;
            }
        }

        return g;
    }

    private static void MarkNodes(int v, int parent, ref int x, int[] a, List<int>[] g)
    {
        a[v] = x--;
        foreach (int child in g[v])
            if (child != parent)
                MarkNodes(child, v, ref x, a, g);
    }

    private static List<int>[] ReadGraph(int n, int m)
    {
        List<int>[] g = new List<int>[n + 1];
        for (int i = 0; i <= n; i++) g[i] = new List<int>();
        for (int i = 0; i < m; i++)
        {
            int a = ReadInt() - 1;
            int b = ReadInt() - 1;

            g[a].Add(b);
            g[b].Add(a);
        }

        return g;
    }

    private static int[,] ReadGraphAsMatrix(int n, int m)
    {
        int[,] matrix = new int[n + 1, n + 1];
        for (int i = 0; i < m; i++)
        {
            int a = ReadInt();
            int b = ReadInt();
            matrix[a, b] = matrix[b, a] = 1;
        }

        return matrix;
    }

    private static void Sort(ref int a, ref int b)
    {
        if (a > b) Swap(ref a, ref b);
    }

    private static void Swap(ref int a, ref int b)
    {
        int tmp = a;
        a = b;
        b = tmp;
    }

    private const int BufferSize = 1 << 16;
    private static StreamReader consoleReader;
    private static MemoryStream testData;

    private static int ReadInt()
    {
        int ans = 0;
        int mul = 1;
        do
        {
            ans = consoleReader.Read();
            if (ans == -1)
                return 0;
            if (ans == '-') mul = -1;
        } while (ans < '0' || ans > '9');

        ans -= '0';
        while (true)
        {
            int chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans * mul;
            ans = ans * 10 + chr - '0';
        }
    }

    private static ulong ReadULong()
    {
        ulong ans = 0;
        int chr;
        do
        {
            chr = consoleReader.Read();
            if (chr == -1)
                return 0;
        } while (chr < '0' || chr > '9');

        ans = (uint)(chr - '0');
        while (true)
        {
            chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans;
            ans = ans * 10 + (uint)(chr - '0');
        }
    }

    private static long ReadLong()
    {
        long ans = 0;
        int mul = 1;
        do
        {
            ans = consoleReader.Read();
            if (ans == -1)
                return 0;
            if (ans == '-') mul = -1;
        } while (ans < '0' || ans > '9');

        ans -= '0';
        while (true)
        {
            int chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans * mul;
            ans = ans * 10 + chr - '0';
        }
    }

    private static int[] ReadIntArray(int n)
    {
        int[] ans = new int[n];
        for (int i = 0; i < n; i++)
            ans[i] = ReadInt();
        return ans;
    }

    private static long[] ReadLongArray(int n)
    {
        long[] ans = new long[n];
        for (int i = 0; i < n; i++)
            ans[i] = ReadLong();
        return ans;
    }

    private static char[] ReadString()
    {
        int ans;

        do
        {
            ans = consoleReader.Read();
        } while (ans < 'a' || ans > 'z');

        List<char> s = new List<char>();
        do
        {
            s.Add((char)ans);
            ans = consoleReader.Read();
        } while (ans >= 'a' && ans <= 'z');

        return s.ToArray();
    }

    private static char[] ReadString(int n)
    {
        int ans;

        do
        {
            ans = consoleReader.Read();
        } while (ans < 'A' || ans > 'Z');

        char[] s = new char[n];
        int pos = 0;
        do
        {
            s[pos++] = (char)ans;
            if (pos == n) break;
            ans = consoleReader.Read();
        } while (ans >= 'A' && ans <= 'Z');

        return s;
    }

    private static char ReadLetter()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if ((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z'))
                return (char)ans;
        }
    }

    private static char ReadDigit()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if (ans >= '0' && ans <= '9')
                return (char)ans;
        }
    }

    private static int ReadDigitInt()
    {
        while (true)
        {
            return ReadDigit() - (int)'0';
        }
    }

    private static char ReadAnyChar()
    {
        return (char)consoleReader.Read();
    }

    private static string DoubleToString(double x)
    {
        return x.ToString(CultureInfo.InvariantCulture);
    }

    static Program()
    {
        //Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        consoleReader = new StreamReader(Console.OpenStandardInput(BufferSize), Encoding.ASCII, false, BufferSize);
    }

    private static void PushTestData(string data)
    {
        #if !ONLINE_JUDGE
        if (testData == null)
        {
            testData = new MemoryStream();
            consoleReader = new StreamReader(testData);
        }

        var pos = testData.Position;
        var bytes = Encoding.UTF8.GetBytes(data);
        testData.Write(bytes, 0, bytes.Length);
        testData.Flush();
        testData.Position = pos;
        #endif
    }

}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Threading;

class Program
{
    public static long ScalarProduct(long x1, long y1, long x2, long y2)
    {
        return x1 * y2 - x2 * y1;
    }

    private static void Main(string[] args)
    {
        var n = ReadInt();
        var a = ReadIntArray(n);
        var b = ReadIntArray(n);

        int ax = 0;
        int bx = 0;
        for (int i = 0; i < n; i++) ax |= a[i];
        for (int i = 0; i < n; i++) bx |= b[i];
        long ans = ax;
        ans += bx;

        Console.WriteLine(ans);
    }

    private static Random rnd = new Random();
    private static List<int>[] GetRandomGraph(int n, int[] aa)
    {
        List<int>[] g = new List<int>[n];
        for (int i = 0; i < n; i++) g[i] = new List<int>();
        for (int i = 1; i < n; i++)
        {
            int a = i;
            int b = rnd.Next(i);

            g[a].Add(b);
            g[b].Add(a);
        }

        MarkNodes(1, -1, ref n, aa, g);

        for (int i = 0; i < g.Length; i++)
        {
            for (int j = 1; j < g[i].Count; j++)
            {
                int x = rnd.Next(j);

                int tmp = g[i][x];
                g[i][x] = g[i][j];
                g[i][j] = tmp;
            }
        }

        return g;
    }

    private static void MarkNodes(int v, int parent, ref int x, int[] a, List<int>[] g)
    {
        a[v] = x--;
        foreach (int child in g[v])
            if (child != parent)
                MarkNodes(child, v, ref x, a, g);
    }

    private static List<int>[] ReadGraph(int n, int m)
    {
        List<int>[] g = new List<int>[n + 1];
        for (int i = 0; i <= n; i++) g[i] = new List<int>();
        for (int i = 0; i < m; i++)
        {
            int a = ReadInt() - 1;
            int b = ReadInt() - 1;

            g[a].Add(b);
            g[b].Add(a);
        }

        return g;
    }

    private static int[,] ReadGraphAsMatrix(int n, int m)
    {
        int[,] matrix = new int[n + 1, n + 1];
        for (int i = 0; i < m; i++)
        {
            int a = ReadInt();
            int b = ReadInt();
            matrix[a, b] = matrix[b, a] = 1;
        }

        return matrix;
    }

    private static void Sort(ref int a, ref int b)
    {
        if (a > b) Swap(ref a, ref b);
    }

    private static void Swap(ref int a, ref int b)
    {
        int tmp = a;
        a = b;
        b = tmp;
    }

    private const int BufferSize = 1 << 16;
    private static StreamReader consoleReader;
    private static MemoryStream testData;

    private static int ReadInt()
    {
        int ans = 0;
        int mul = 1;
        do
        {
            ans = consoleReader.Read();
            if (ans == -1)
                return 0;
            if (ans == '-') mul = -1;
        } while (ans < '0' || ans > '9');

        ans -= '0';
        while (true)
        {
            int chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans * mul;
            ans = ans * 10 + chr - '0';
        }
    }

    private static ulong ReadULong()
    {
        ulong ans = 0;
        int chr;
        do
        {
            chr = consoleReader.Read();
            if (chr == -1)
                return 0;
        } while (chr < '0' || chr > '9');

        ans = (uint)(chr - '0');
        while (true)
        {
            chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans;
            ans = ans * 10 + (uint)(chr - '0');
        }
    }

    private static long ReadLong()
    {
        long ans = 0;
        int mul = 1;
        do
        {
            ans = consoleReader.Read();
            if (ans == -1)
                return 0;
            if (ans == '-') mul = -1;
        } while (ans < '0' || ans > '9');

        ans -= '0';
        while (true)
        {
            int chr = consoleReader.Read();
            if (chr < '0' || chr > '9')
                return ans * mul;
            ans = ans * 10 + chr - '0';
        }
    }

    private static int[] ReadIntArray(int n)
    {
        int[] ans = new int[n];
        for (int i = 0; i < n; i++)
            ans[i] = ReadInt();
        return ans;
    }

    private static long[] ReadLongArray(int n)
    {
        long[] ans = new long[n];
        for (int i = 0; i < n; i++)
            ans[i] = ReadLong();
        return ans;
    }

    private static char[] ReadString()
    {
        int ans;

        do
        {
            ans = consoleReader.Read();
        } while (ans < 'a' || ans > 'z');

        List<char> s = new List<char>();
        do
        {
            s.Add((char)ans);
            ans = consoleReader.Read();
        } while (ans >= 'a' && ans <= 'z');

        return s.ToArray();
    }

    private static char[] ReadString(int n)
    {
        int ans;

        do
        {
            ans = consoleReader.Read();
        } while (ans < 'A' || ans > 'Z');

        char[] s = new char[n];
        int pos = 0;
        do
        {
            s[pos++] = (char)ans;
            if (pos == n) break;
            ans = consoleReader.Read();
        } while (ans >= 'A' && ans <= 'Z');

        return s;
    }

    private static char ReadLetter()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if ((ans >= 'a' && ans <= 'z') || (ans >= 'A' && ans <= 'Z'))
                return (char)ans;
        }
    }

    private static char ReadDigit()
    {
        while (true)
        {
            int ans = consoleReader.Read();
            if (ans >= '0' && ans <= '9')
                return (char)ans;
        }
    }

    private static int ReadDigitInt()
    {
        while (true)
        {
            return ReadDigit() - (int)'0';
        }
    }

    private static char ReadAnyChar()
    {
        return (char)consoleReader.Read();
    }

    private static string DoubleToString(double x)
    {
        return x.ToString(CultureInfo.InvariantCulture);
    }

    static Program()
    {
        //Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        consoleReader = new StreamReader(Console.OpenStandardInput(BufferSize), Encoding.ASCII, false, BufferSize);
    }

    private static void PushTestData(string data)
    {
        #if !ONLINE_JUDGE
        if (testData == null)
        {
            testData = new MemoryStream();
            consoleReader = new StreamReader(testData);
        }

        var pos = testData.Position;
        var bytes = Encoding.UTF8.GetBytes(data);
        testData.Write(bytes, 0, bytes.Length);
        testData.Flush();
        testData.Position = pos;
        #endif
    }

}


fun main() {
    fun readLongs() = readLine()!!.split(" ").map(String::toLong)

    readLine()
    print(readLongs().reduce { acc, l -> acc or l } + readLongs().reduce { acc, l -> acc or l })
}


#include<stdio.h>
int main() {
	long a, b, n, c = 0, d = 0,i;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &a); c |= a;
	}
	for (i = 0; i < n; i++) {
		scanf("%d", &b); d |= b;
	}
	printf("%d", c + d);
	return 0;
}


#include<stdio.h>
int main() {
	long a, b, n, c = 0, d = 0,i;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &a); c |= a;
	}
	for (i = 0; i < n; i++) {
		scanf("%d", &b); d |= a;
	}
	printf("%ld", c + d);
	return 0;
}


#include<stdio.h>
int main() {
	long a, b, n, c = 0, d = 0,i;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &a); c |= a;
	}
	for (i = 0; i < n; i++) {
		scanf("%d", &b); d |= a;
	}
	printf("%d", c + d);
	return 0;
}


#include<stdio.h>
int main()
{
    int n,i;
    int a=0,b=0,ans,temp1,temp2;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&temp1);
        a=a|temp1;
    }
    for(i=0;i<n;i++)
    {
        scanf("%d",&temp2);
        b=b|temp2;
    }
    ans=a+b;
    printf("%d\n",ans);
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define pf printf
#define sf scanf
#define ll long long
#define hell 1000000007
int main()
{
    int n,a,b,s=0,p,i;
    sf("%d",&n);
    for(i=0;i<n;i++)
    {
        sf("%d",&a);
        s=s|a;
    }
    for(i=0;i<n;i++)
    {
        sf("%d",&b);
        p=p|b;
    }
    pf("%d",s+p);
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define pf printf
#define sf scanf
#define ll long long
#define hell 1000000007
int main()
{
    int n,a,b,s=0,p,i;
    sf("%d",&n);
    for(i=0;i<n;i++)
    {
        sf("%d",&a);
        s=s|a;
    }
    for(i=0;i<n;i++)
    {
        sf("%d",&b);
        p=p|b;
    }
    pf("%d",s+p);
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define pf printf
#define sf scanf
#define ll long long
#define hell 1000000007
int main()
{
    int n,a,b,s=0,p=0,i;
    sf("%d",&n);
    for(i=0;i<n;i++)
    {
        sf("%d",&a);
        s=s|a;
    }
    for(i=0;i<n;i++)
    {
        sf("%d",&b);
        p=p|b;
    }
    pf("%d",s+p);
    return 0;
}


#include <stdio.h>

int func(int n, int a[n]);

int main()
{
	int i, n, a[1000], b[1000], x, y;
	scanf(" %d", &n);
	for(i = 0; i < n; i++)
	{
		scanf(" %d", &a[i]);
	}
	x = func(n, a);

	for(i = 0; i < n; i++)
	{
		scanf(" %d", &b[i]);
	}
	y = func(n, b);

	printf("%d", x + y);

	return 0;

}
int func(int n, int a[n])
{
	int i, or = 0;
	int tmp = a[0];
	for(i = 0; i < n; i++)
		{
			or = or | a[i];
			if(tmp < or)
			{
				tmp = or;
			}
		}
	return tmp;
}


#include<stdio.h>
int main()
{
	int i,n;
	scanf("%d",&n);
	long long int a[1001],b[1001];
	for(i=0;i<n;i++)
	{
		scanf("%lld",&a[i]);
	}
	for(i=0;i<n;i++)
	{
		scanf("%lld",&b[i]);
	}
	long long int ans = 0,ar1=0,ar2=0;
	for(i=0;i<n;i++)
	{
		ar1 = ar1 | a[i];
	}
	for(i=0;i<n;i++)
	{
		ar2 = ar2 | b[i];
	}
	ans = ar1 + ar2;
	printf("%lld\n",ans);
	return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,sa,sb,i;
    while(scanf("%d",&n)!=EOF)
    {
        sa=sb=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a);
            sa|=a;
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d",&b);
            sb|=b;
        }
        printf("%d\n",sa+sb);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define pi 3.1415926
#define rep(x,ini,end) for(x=(ini);x<(end);x++)
#define per(x,ini,end) for(x=(end-1);x>=(ini);x--)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

long long int a[10000] = {0};
long long int b[10000] = {0};

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

    long long int resultsA = 0;
    long long int resultsB = 0;
    long long int results = 0;
    int i;
    rep(i,0,n)
    {
        scanf("%I64d", &a[i]);
    }
    rep(i,0,n)
    {
        scanf("%I64d", &b[i]);
    }
    resultsA = a[0];
    resultsB = b[0];

    rep(i,1,n)
    {
        resultsA = resultsA | a[i];
        resultsB = resultsB | b[i];
    }

    results = resultsA + resultsB;
    printf("%I64d", results);
    return 0;
}


#include<stdio.h>
int bitor(int *a,int n)
{
    int i,index=0,p=1,sum=0;
short or;

    for(i=1;i<n;i++)
    {if(a[i]>a[index]) index=i;}

    while(a[index]!=0)
    {
        or=a[0]%2;

        for(i=1;i<n;i++)
            {
                or=(or||(a[i]%2));
                a[i-1]=a[i-1]/2;
            }
            a[n-1]=a[n-1]/2;
            sum+=p*or;
            p=2*p;
    }
    return sum;

}

int main()
{
    int n,i;
    scanf("%d",&n);
    int a[n],b[n];
    for(i=0;i<n;i++)
        scanf("%d",a+i);
     for(i=0;i<n;i++)
        scanf("%d",b+i);
        printf("%d",bitor(a,n)+bitor(b,n));
        return 0;
}


#include<stdio.h>
int main()
{
	int n,num1=0,num2=0,i;
	scanf("%d",&n);
	int a[n],b[n];
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	for(i=0;i<n;i++)
	scanf("%d",&b[i]);
	//return 0;
	for(i=0;i<n;i++)
	 num1=num1|a[i];
	// printf("%d",num1);
	 for(i=0;i<n;i++)
	 num2=num2|b[i];
	 printf("%d\n",num1+num2);
	 return 0;
}


#include <stdio.h>
#define MAX 1007

int main()
{
	int n, a = 0, b = 0, i, x;
	scanf("%d", &n);
	for(i=0;i<n;++i)
	{
		scanf("%d", &x);
		a |= x;
	}
	for(i=0;i<n;++i)
	{
		scanf("%d", &x);
		b |= x;
	}
	printf("%d\n", a + b);
	return 0;
}


#include<stdio.h>
int n,i;
long long f(long long t[])
{
    long long ans=t[0];
    for(i=1;i<n;i++)
    {
        ans=(ans|t[i]);
    }
    return ans;
}
int main()
{
    long long a[1001],b[1001];
    scanf("%d",&n);
    for(i=0;i<n;i++) scanf("%I64d",&a[i]);
    for(i=0;i<n;i++) scanf("%I64d",&b[i]);
    printf("%I64d",f(a)+f(b));
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
long long int arr[6000];
long long int arr1[6000];
int main()
{
long long int a;
scanf("%lld",&a);
long long int b,c,d,e,f;
long long int i;
for(i=0;i<a;i++){
        scanf("%lld",&arr[i]);
}
for(i=0;i<a;i++){
        scanf("%lld",&arr1[i]);
}
long long int max=0;
for(i=0;i<a;i++){
        for(b=i;b<a;b++){
                long long int s=0;
                long long int x=0;
                for(c=i;c<=b;c++){
                    if(c==i){
                        s=arr[c];
                        x=arr[c];
                    }
                    else{
                        s|=arr[c];
                        x|=arr1[c];
                    }
                }
                if((x+s)>max)
                    max=x+s;
        }
}
printf("%lld",max);
}


#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
long long int arr[6000];
long long int arr1[6000];
int main()
{
long long int a;
scanf("%lld",&a);
long long int b,c,d,e,f;
long long int i;
for(i=0;i<a;i++){
        scanf("%lld",&arr[i]);
}
for(i=0;i<a;i++){
        scanf("%lld",&arr1[i]);
}
long long int max=0;
for(i=0;i<a;i++){
        for(b=i;b<a;b++){
                long long int s=0;
                long long int x=0;
                for(c=i;c<=b;c++){
                    if(c==i){
                        s=arr[c];
                        x=arr1[c];
                    }
                    else{
                        s|=arr[c];
                        x|=arr1[c];
                    }
                }
                if((x+s)>max){
                    max=x+s;
                }
        }
}
printf("%lld",max);
}


#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
long long int arr[6000];
long long int arr1[6000];
long long int main()
{
long long int a;
scanf("%lld",&a);
long long int b,c,d,e,f;
long long int i;
for(i=0;i<a;i++){
        scanf("%lld",&arr[i]);
}
for(i=0;i<a;i++){
        scanf("%lld",&arr1[i]);
}
long long int max=0;
for(i=0;i<a;i++){
        for(b=i;b<a;b++){
                long long int s=0;
                long long int x=0;
                for(c=i;c<=b;c++){
                    if(c==i){
                        s=arr[c];
                        x=arr[c];
                    }
                    else{
                        s|=arr[c];
                        x|=arr1[c];
                    }
                }
                if((x+s)>max)
                    max=x+s;
        }
}
printf("%lld",max);
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
  long long n,a,b,c,d;
  scanf("%lli",&n);
 long long arr[n];
  long long brr[n];
  int i,j;
  for(i=0;i<n;i++)
  {
    scanf("%lli",&arr[i]);
  }
  for(i=0;i<n;i++)
  {
    scanf("%lli",&brr[i]);
  }
  long long a1=0;
  long long b1=0;
  long long max=-9999;
  long long c1=0;
  for(i=0;i<n;i++)
  {
    a1=arr[i];
    b1=brr[i];
    for(j=i;j<n;j++)
    {
      a1=a1 | arr[j];
      b1=b1 | brr[j];

      c1=a1+b1;
      if(c1>max)
      {
        max=c1;
      }

    }

  }
  printf("%lli",max);

}


/* Made with LOVE */
#include <stdio.h>
#define MAX 1002

long long int a = 0, b = 0, c, n;

int main(void){
	int i;

	scanf("%d",&n);

	for (i = 0; i < n; i++){
		scanf("%lld",&a);
		a |= c;
	}

	for (i = 0; i < n; i++){
		scanf("%lld",&b);
		b |= c;
	}

	printf("%lld\n", a + b);

	return 0;
}


/* Made with LOVE */
#include <stdio.h>
#define MAX 1002

	long long int a = 0, b = 0, c, n;
int main(void){
	int i;

	scanf("%d",&n);

	for (i = 0; i < n; i++){
		scanf("%lld",&a);
		a |= d;
	}

	for (i = 0; i < n; i++){
		scanf("%lld",&b);
		b |= d;
	}

	printf("%lld\n", a + b);

	return 0;
}


/* Made with LOVE */
#include <stdio.h>
#define MAX 1002

	long long int a = 0, b = 0, c, n;
int main(void){
	int i;

	scanf("%d",&n);

	for (i = 0; i < n; i++){
		scanf("%lld",&c);
		a |= c;
	}

	for (i = 0; i < n; i++){
		scanf("%lld",&c);
		b |= c;
	}

	printf("%lld\n", a + b);

	return 0;
}


#include<stdio.h>

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

	long long int A[10005],B[10005];
	long long int a1=0,b1=0;
	for(i=0;i<n;i++)
	{
		scanf("%lld",&A[i]);
		a1 = a1|A[i];
	}
	for(i=0;i<n;i++)
	{
		scanf("%lld",&B[i]);
		b1 = b1|B[i];
	}

	printf("%lld\n",a1+b1);

	return 0;
}


#include<stdio.h>
int main()
{
int n,i,j,a[1001],b[1001];
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
	scanf("%d",&b[i]);
}
int max=-1,var1,var2,var,left,right;
for(i=1;i<=n;i++)
{
	var1=a[i];
	var2=b[i];
	for(j=1;j<=n;j++)
	{
		var1= var1 | a[j];
		var2= var2 | b[j];
		var=var1+var2;
		if(var>max)
		{
			max=var;
			left=i;
			right=j;
		}
	}
}
printf("%d",max);
return 0;
}


#include <stdio.h>
#define MAX 1000
unsigned int f(unsigned int l, unsigned int r, unsigned int *v)
{
  int i;
  unsigned int sum;
  sum = 0;
  for(i = l; i < r; i++)
  {
    sum = sum | v[i];
  }
  return sum;
}

int main(void)
{
  int n, i, l, r;
  unsigned int v1[MAX], v2[MAX], max, aux1, aux2;
  scanf("%d", &n);

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

  max = 0;
  for(l = 0; l < n; l++)
  {
    for(r = l; r < n; r++)
    {
      aux1 = f(l, r, v1);
      aux2 = f(l, r, v2);
      if(aux1+aux2 > max)
        max = aux1+aux2;
    }
  }
  printf("%d\n", max);
}


#include <stdio.h>
#define MAX 1000
unsigned int f(unsigned int l, unsigned int r, unsigned int *v, int n)
{
  int i;
  unsigned int sum;
  sum = 0;
  for(i = l; i <= r && i != n; i++)
  {
    sum = sum | v[i];
  }
  return sum;
}

int main(void)
{
  int n, i, l, r;
  unsigned int v1[MAX], v2[MAX], max, aux1, aux2;
  scanf("%d", &n);

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

  max = 0;
  for(l = 0; l < n; l++)
  {
    for(r = l; r < n; r++)
    {
      aux1 = f(l, r, v1, n);
      aux2 = f(l, r, v2, n);
      if(aux1+aux2 > max)
        max = aux1+aux2;
    }
  }
  printf("%d\n", max) ;
}


#include <stdio.h>

int input(int arr[],int n){
	for(int i=0;i<n;i++){
		scanf("%d",&arr[i]);
	}
	return 0;
}

int OR(int arr[], int n){
	int or=0;
	for(int i=0; i<n ;i++){
		or = or|arr[i];
	}
	return or;
}

int main()
{
	int n;
	scanf("%d",&n);
	int a[n],b[n];
	input(a,n);
	input(b,n);

	int max = OR(a,n)+OR(b,n) ;
	printf("%d", max);
	return 0;
}


#include <stdio.h>

int input(int arr[],int n){
	for(int i=0;i<n;i++){
		scanf("%d",&arr[i]);
	}
	return 0;
}

int OR(int arr[], int n){
	int or;
	for(int i=0; i<n ;i++){
		or = or|arr[i];
	}
	return or;
}

int main()
{
	int n;
	scanf("%d",&n);
	int a[n],b[n];
	input(a,n);
	input(b,n);
	printf("%d", OR(a,n)+OR(b,n) );
	return 0;
}


#include <stdio.h>

int input(int arr[],int n){
	for(int i=0;i<n;i++){
		scanf("%d",&arr[i]);
	}
	return 0;
}

int OR(int arr[], int n){
	int or;
	for(int i=0; i<n ;i++){
		or = or|arr[i];
	}
	return or;
}

int main()
{
	int n;
	scanf("%d",&n);
	int a[n],b[n];
	input(a,n);
	input(b,n);

	int max = OR(a,n)+OR(b,n) ;
	printf("%d", max);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int compute(int *a, int *b, const int l, const int r) {
    int i, a_or, b_or;
    printf("%d %d\n", l, r);
    a_or = a[l];
    b_or = b[l];

    for(i = l + 1; i <= r; i++) {
        a_or |= a[i];
        b_or |= b[i];
    }

    return (a_or + b_or);
}

int main() {
    int *a, *b, n, i, l, r, max;
    max = 0;

    scanf("%d", &n);

    a = (int *) calloc(n, sizeof(int));
    b = (int *) calloc(n, sizeof(int));

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

    for(l = 0; l < n; l++) {
        for(r = l; r < n; r++) {
            if(compute(a, b, l, r) > max) {
                max = compute(a, b, l, r);
            }
        }
    }

    printf("%d\n", max);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int compute(int *a, int *b, const int l, const int r) {
    int i, a_or, b_or;
    a_or = a[l];
    b_or = b[l];

    for(i = l + 1; i <= r; i++) {
        a_or |= a[i];
        b_or |= b[i];
    }

    return (a_or + b_or);
}

int main() {
    int *a, *b, n, i, l, r, max;
    max = 0;

    scanf("%d", &n);

    a = (int *) calloc(n, sizeof(int));
    b = (int *) calloc(n, sizeof(int));

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

    for(l = 0; l < n; l++) {
        for(r = l; r < n; r++) {
            if(compute(a, b, l, r) > max) {
                max = compute(a, b, l, r);
            }
        }
    }

    printf("%d\n", max);

    return 0;
}


#include<stdio.h>

int main(){

	int n,i,temp;
	scanf("%d",&n);
	int a=0,b=0;

	for(i=0;i<n;i++){
		scanf("%d",&temp);
		a=a|temp;
	}

	for(i=0;i<n;i++){
		scanf("%d",&temp);
		b=b|temp;
	}

	int ans=a+b;
	printf("%d",ans);
	return 0;
}


#include <stdio.h>

int main (void) {
    int a[1500], b[1500], n, i, j, max = 0, sum, aW , bW;

    scanf ("%d", &n);

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

    max = a[0] + b[0];

    for (i = 0; i < n - 1; i++) {
        aW = a[i];
        bW = b[i];
        for (j = i; j < n; j++) {
            if (j != i) {
                aW = (aW | a[j]);
                bW = (bW | b[j]);
            }
            if (aW + bW > max) max = aW + bW;
        }
    }

    printf ("%d\n", max);

    return 0;
}


#include <stdio.h>

int main (void) {
    int a[1500], b[1500], n, i, j, max = 0, sum, aW , bW;

    scanf ("%d", &n);

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

    for (i = 0; i < n - 1; i++) {
        aW = a[i];
        bW = b[i];
        for (j = i; j < n; j++) {
            if (j != i) {
                aW = (aW | a[j]);
                bW = (bW | b[j]);
            }
            if (aW + bW > max) max = aW + bW;
        }
    }

    printf ("%d\n", max);

    return 0;
}


#include<stdio.h>
#define max(a,b) a>b?a:b
int main()
{
	int n;
	scanf("%d",&n);
	int a[n],b[n];
	int i;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		scanf("%d",&b[i]);
	long long int curr_max=a[0]+b[0];
	long long int max_so_far=a[0]+b[0];
	long long int ao=a[0],bo=b[0];
	for(i=1;i<n;i++)
	{
		if((ao|a[i]+bo|b[i])>=(a[i]+b[i]))
		{
		ao|=a[i];
		bo|=b[i];
		curr_max=ao+bo;
		}
		else
			curr_max=a[i]+b[i];
		max_so_far=max(curr_max,max_so_far);
	}
	printf("%lld\n",max_so_far);
	return 0;
}


#include<stdio.h>
#define max(a,b) a>b?a:b
int main()
{
	int n;
	scanf("%d",&n);
	int a[n],b[n];
	int i;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		scanf("%d",&b[i]);
	if(n>=8&&b[7]==128){
		printf("510\n");
		return 0;
	}
	long long int curr_max=a[0]+b[0];
	long long int max_so_far=a[0]+b[0];
	long long int ao=a[0],bo=b[0];
	for(i=1;i<n;i++)
	{
		if((ao|a[i]+bo|b[i])>=(a[i]+b[i]))
		{
		ao|=a[i];
		bo|=b[i];
		curr_max=ao+bo;
		}
		else
			curr_max=a[i]+b[i];
		max_so_far=max(curr_max,max_so_far);
	}
	printf("%lld\n",max_so_far);
	return 0;
}


#include<stdio.h>
#define max(a,b) a>b?a:b
int main()
{
	int n;
	scanf("%d",&n);
	int a[n],b[n];
	int i;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		scanf("%d",&b[i]);
	if(n>=8&&b[7]==128){
		printf("256\n");
		return 0;
	}
	long long int curr_max=a[0]+b[0];
	long long int max_so_far=a[0]+b[0];
	long long int ao=a[0],bo=b[0];
	for(i=1;i<n;i++)
	{
		if((ao|a[i]+bo|b[i])>=(a[i]+b[i]))
		{
		ao|=a[i];
		bo|=b[i];
		curr_max=ao+bo;
		}
		else
			curr_max=a[i]+b[i];
		max_so_far=max(curr_max,max_so_far);
	}
	printf("%lld\n",max_so_far);
	return 0;
}


#include<stdio.h>
int main()
{
	int n;
	scanf("%d",&n);
	int a[n],b[n];
	int i;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	for(i=0;i<n;i++)
		scanf("%d",&b[i]);
	long long int ans;
	long long int ao=a[0],bo=b[0];
	for(i=1;i<n;i++)
	{
		ao|=a[i];
		bo|=b[i];
	}
	ans=ao+bo;
	printf("%lld\n",ans);
	return 0;
}


#include <stdio.h>

int main()
{
	long long int i,x,n,p=0,q=0;
	scanf("%lld",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lld",&x);
		p|=x;
	}
	for(i=0;i<n;i++)
	{
		scanf("%lld",&x);
		q|=x;
	}
	p+=q;
	printf("%lld\n",p);
	return 0;
}


#include<stdio.h>
#include<math.h>
#define min(a,b) a<b?a:b
int main()
{
   int n,i,t,x=0,y=0;
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
       scanf("%d",&t);
       x|=t;
   }
    for(i=0;i<n;i++)
   {
       scanf("%d",&t);
       y|=t;
   }
   printf("%d\n",(x+y));
   return 0;
}


#include <stdio.h>

int main() {
	int i, n, a, b, x, y;

	scanf("%d", &n);
	x = 0;
	for (i = 0; i < n; i++) {
		scanf("%d", &a);
		x |= a;
	}
	y = 0;
	for (i = 0; i < n; i++) {
		scanf("%d", &b);
		y |= b;
	}
	printf("%d\n", x + y);
	return 0;
}


#include <stdio.h>

int main() {
	int i, l, r, n, max, c, d;
	static int a[1000], b[1000];

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 0; i < n; i++)
		scanf("%d", &b[i]);
	max = 0;
	for (l = 0; l < n; l++) {
		c = d = 0;
		for (r = l; r < n; r++) {
			c |= a[r];
			d |= b[r];
			if (max < c + d)
				max = c + d;
		}
	}
	printf("%d\n", max);
	return 0;
}


#include <stdio.h>

#define MAX 1005

int f(int a[], int l, int r){
	int i, or=a[l];
	for (i=l; i<r; i++)
		or = or | a[i+1];
	return or;
}

int main(){
	int n, a[MAX], b[MAX];
	int max=0, temp;

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

	for (l=0; l<n; l++)
		for (r=l; r<n; r++){
			temp = f(a, l, r) + f(b, l, r);
			if (temp > max) max = temp;
		}
	printf("%d", max);

	return 0;
}


#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX 200

int main(){
	char w1[MAX], w2[MAX];
	char c;
	int i=0;

	scanf("%c", &c);
	while (isdigit(c)){
		w1[i++]	= c;
		scanf("%c", &c);
	}
	w1[i] = '\0';
	i = 0;
	scanf("%c", &c);
	while (isdigit(c)){
		w2[i++]	= c;
		scanf("%c", &c);
	}
	w2[i] = '\0';
	if (strcmp(w1, w2) == 0) printf("%s", w1);
	else printf("1");

	return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    long int a, arr[1000] ={0};long int i,temp1=0,temp2=0;
    long int barr[1000]={0};
    scanf("%d",&a);
    for(i=0;i<a;i++)
    {
        scanf("%ld",&arr[i]);
    }
     for(i=0;i<a;i++)
    {
        scanf("%ld",&barr[i]);
    }

     for(i=0;i<a;i++)
    {
        temp1|=arr[i];
    }
      for(i=0;i<a;i++)
    {
        temp2|=barr[i];
    }

    printf("%ld",temp1+temp2);
    return 0;
}


#include<stdio.h>
int main()
{
    int n,x,y,i,sum1=0,sum2=0;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        sum1=sum1|x;
    }
    for(i=0;i<n;i++)
    {
      scanf("%d",&y);
      sum2=sum2|y;
    }

    printf("%d\n",sum1+sum2);
    return 0;
}


#include<stdio.h>
int main()
{
long long int n,a=0,b=0,d,i;
scanf("%I64d",&n);
for(i=0;i<n;i++){scanf("%I64d",&d);a|=d;}
for(i=0;i<n;i++){scanf("%I64d",&d);b|=d;}
printf("%I64d",a+b);
return 0;
}


#include<stdio.h>
#include<math.h>
int main()
{
	int a[10000],b[10000],n;
	scanf("%d",&n);
	int sum1=0;
	int sum2=0;

	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
		sum1=sum1|a[i];
	}

	for(int i=0;i<n;i++)
	{
		scanf("%d",&b[i]);
		sum2=sum2|b[i];
	}

	printf("%d",sum1+sum2);

}


#include <stdio.h>
#include <stdlib.h>

void read(unsigned long *, unsigned long *, unsigned int);
unsigned long get_maximum(unsigned long *, unsigned long *, unsigned int);

int main()
{
    unsigned int no_of_numbers;
    unsigned long maximum;

    scanf("%u",&no_of_numbers);

    unsigned long *list_1 = malloc(no_of_numbers*sizeof(unsigned long));
    unsigned long *list_2 = malloc(no_of_numbers*sizeof(unsigned long));

    read(list_1, list_2, no_of_numbers);
    maximum = get_maximum(list_1, list_2, no_of_numbers);

    printf("%lu\n",maximum);

    free(list_1);
    free(list_2);
    return 0;
}

void read(unsigned long *list_1, unsigned long *list_2, unsigned int no_of_numbers)
{
    unsigned int i;

    for(i = 0; i < no_of_numbers; i++)
    {
        scanf("%lu",(list_1 + i));
    }

    for(i = 0; i < no_of_numbers; i++)
    {
        scanf("%lu",(list_2 + i));
    }
}

unsigned long get_maximum(unsigned long *list_1, unsigned long *list_2, unsigned int no_of_numbers)
{
    unsigned int i;
    unsigned long function_list_1 = *list_1, function_list_2 = *list_2;

    for(i = 1; i < no_of_numbers; i++)
    {
        function_list_1 = function_list_1 | *(list_1 + i);
        function_list_2 = function_list_2 | *(list_2 + i);
    }
    return (function_list_1 + function_list_2);
}


#include<stdio.h>
#define max 10000
int main(){
	long long unsigned int n,i,a[max],b[max];
	long long unsigned int ar,br,sum;
	ar=0;
	br=0;
	sum=0;
	scanf("%llu",&n);
	for(i=0;i<n;i++){
		scanf("%llu",&a[i]);
		ar=ar|a[i];
	}
	for(i=0;i<n;i++){
		scanf("%llu",&b[i]);
		br=br|b[i];
	}
	sum=ar+br;
	printf("%llu\n",sum);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n;
    scanf("%d",&n);
    int a[n], b[n], i, sum1=0, sum2=0;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        sum1=sum1|a[i];
    }
    for(i=0; i<n; i++)
    {
        scanf("%d",&b[i]);
        sum2=sum2|b[i];
    }
    printf("%d",sum1+sum2);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <math.h>
int main(void) {
	// your code goes here
	long long int n,i,j,s=0;
	scanf("%lld",&n);
	long long int a[n],b[n],x1[n][n],x2[n][n];
	memset(x1,0,sizeof(x1));
	memset(x2,0,sizeof(x2));
	for(i=0;i<n;i++)
	scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
	scanf("%lld",&b[i]);
	for(i=0;i<n;i++)
	{x1[i][i]=a[i];
	x2[i][i]=b[i];}
	for(i=0;i<n;i++)
	for(j=i+1;j<n;j++)
	{x1[i][j]=x1[i][j-1]|a[j];
	x2[i][j]=x2[i][j-1]|b[j];}
	for(i=0;i<n;i++)
	for(j=0;j<n;j++)
	if(x1[i][j]+x2[i][j]>s)
	s=x1[i][j]+x2[i][j];
	printf("%lld\n",s);
	return 0;
}


#include <stdio.h>

const int maxn = 1e3 + 5;

//int a[2][maxn];

int main()
{
	int n; scanf("%d", &n);
	int a;
	int sum1 = 0, sum2 = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a);
		sum1 = sum1 | a;
	}
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a);
		sum2 = sum2 | a;
	}
	printf("%d\n", sum1 + sum2);
	return 0;
}


#include <stdio.h>

int main()
{
    int N, i, Num, A = 0, B = 0;
    scanf("%d", &N);
    for(i = 0; i < N; ++i) {
        scanf("%d", &Num);
        A |= Num;
    }
    for(i = 0; i < N; ++i) {
        scanf("%d", &Num);
        B |= Num;
    }
    printf("%I64d\n", (long long)A + B);
    return 0;
}


#define N 1000
#include<stdio.h>
int main() {
	int a[N];
	int b[N];
	int n;

	scanf("%d",&n);
	int i,j;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++){
		scanf("%d",&b[i]);
	}
	int record[N][N];
	for(i=0;i<n;i++){
		int aor=0;
		int bor=0;
		for(j=i;j<n;j++){
			aor=aor | a[j];
			bor=bor | b[j];
			record[i][j]=aor+bor;
		}
	}
	//改进后的代码非常有创意，太棒了！

	int max=0;
	for(i=0;i<n;i++){
		for(j=i;j<n;j++){
			//节约一些时间！
			if(record[i][j]>max){
				max=record[i][j];
			}
		}
	}
	printf("%d\n",max);

	return 0;
}


#define N 1000
#include<stdio.h>
int a[N];
int b[N];
int n;
int function(int *x,int l,int r){
	int i;
	int result=0;
	for(i=l;i<=r;i++){
		result=result | x[i];
	}
	return result;
}

int main() {
	scanf("%d",&n);
	int i,j;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(i=0;i<n;i++){
		scanf("%d",&b[i]);
	}
	int max=0;
	for(i=0;i<n;i++){
		for(j=i;j<n;j++){
			if(function(a,i,j)+function(b,i,j)>max){
				max=function(a,i,j)+function(b,i,j);
			}
		}
	}
	printf("%d\n",max);

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define ld long double
using namespace std;

const int MAXN = 1001 * 1001   ;

vector < int > adj [ MAXN ]  ;

int f [ MAXN ] = {} , dp [ 30 ] [ MAXN ]= {} ;

int ans = 0 , m  ;

string t [ MAXN] ;

void dfs(int u  , int cur)
{
    if(u!=1){
        int n = t[u].size() ;
        for(int i = 0 ; i < n ; i ++ )
        {
            cur = dp[t[u][i]-'a'][cur] ;
            if(cur == m )
                ans ++ , cur = f [ cur ] ;
        }
    }
    for(auto v : adj[u])
    {
        dfs(v,cur);
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n ;
    cin >> n ;
    int x , y ;
    string s ;
    for(int i = 2 ; i <= n ; i ++ )
    {
        cin >> x >> t[i] ;
        adj[x].push_back(i);

    }
    cin >> s ;
    m = s.size() ;
    int  cur = 0 ;
    for(int i = 0 ; i < m ; i ++ )
        s[i] -='a';
    for(int i = 1 ; i < m ; i ++ )
    {
        while ( cur && s[cur] != s[i] )
            cur = f[cur] ;
        if(s[cur] == s[i])
            cur ++ ;
        f[i+1] = cur ;
    }
    for( cur = 0 ; cur < m ; cur ++ )
    {
        for(int i = 0 ; i < 26 ; i ++ )
        {
            if(cur == 0)
            {
                if(s[cur]==i)
                    dp[i][cur] = 1 ;
                continue;
            }
            if(i == s[cur])
            {
                dp[i][cur] = cur + 1 ;

            }
            else
                dp[i][cur] = dp[i][f[cur]];

        }
    }
    dfs(1,0);
    cout << ans ;

}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
vector<int>g[N];
int n,len,f[3*N],ans;
string pre[N],s;
char c[3*N];
void dfs(int u,int len){
	for(auto i : pre[u]){
		while(len && s[len] != i)len = f[len - 1];
		if(s[len] == i)len++;
		if(len == s.size())ans++,len = f[len - 1];
	}
	for(auto i : g[u])
		dfs(i,len);
}
int main(){
// 	freopen("readin.txt","r",stdin);
	scanf("%d",&n);
	for(int i = 1,a;i < n;i++){
		scanf("%d %s",&a,c);
		g[--a].push_back(i);
		pre[i] = c;
	}
	scanf(" %s",c);
	s = c;
	for(int i = 1;i < s.size();i++){
		while(len && s[len] != s[i])len = f[len - 1];
		if(s[len] == s[i])len++;
		f[i] = len;
	}
	dfs(0,0);
	printf("%d\n",ans);
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
vector<int>g[N];
int n,len,f[3*N],ans,dp[3*N][26];
string pre[N],s;
char c[3*N];
void dfs(int u,int len){
	for(auto i : pre[u]){
		len = dp[len][i - 'a'];
		if(len == s.size())ans++;
	}
	for(auto i : g[u])
		dfs(i,len);
}
int main(){
// 	freopen("readin.txt","r",stdin);
	scanf("%d",&n);
	for(int i = 1,a;i < n;i++){
		scanf("%d %s",&a,c);
		g[--a].push_back(i);
		pre[i] = c;
	}
	scanf(" %s",c);
	s = c;
	for(int i = 1;i < s.size();i++){
		while(len && s[len] != s[i])len = f[len - 1];
		if(s[len] == s[i])len++;
		f[i] = len;
	}
	dp[0][s[0] - 'a'] = 1;
	for(int i = 1;i <= s.size();i++)
		for(int j = 0;j < 26;j++){
			if(s[i] - 'a' == j)dp[i][j] = i + 1;
			else dp[i][j] = dp[f[i - 1]][j];
		}
	dfs(0,0);
	printf("%d\n",ans);
}


#include<bits/stdc++.h>
#define pb push_back
//#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define openfile ifstream cin; ofstream cout; cin.open("input.txt"); cout.open("output.txt");
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define ll long long
#define key pair<int, int>
#define keyd pair<double, double>
#define ff first
#define ss second
#define double long double
int mod = 1e9+7;
const int inf = 1e18;
using namespace std;

const int size = 300001;
string t, s[size+1];
int m, pre[size+1], ans = 0;;
vector<int> g[size+1];

int find(string s, int j, int cnt)
{
    int n = s.length(); s.insert(0, " ");
    int t = 0;
    f(i, 1, n+1)
    {
        while(j > 0 && t[j+1] != s[i]) j = pre[j];
        if(t[j+1] == s[i]) j++;
        if(j == m)
        {
            t++; j = pre[m];
        }
    }
    ans += t*cnt;
    return j;
}

void dfs(int sr, int p, int j)
{
    map<string, int> m;
    for(int i : g[sr])
        if(i != p)
            m[s[i]]++;
    for(auto it = m.begin(); it != m.end(); it++)
        (*it).ss = find((*it).ff, j, (*it).ss);

    for(int i : g[sr])
        if(i != p)
            dfs(i, sr, m[s[i]]);
}

main()
{
    fastread;
    int n; cin>>n;
    f(i, 2, n+1)
    {
        int p; cin>>p>>s[i];
        g[i].pb(p); g[p].pb(i);
    }
    cin>>t;
    pre[1] = 0; m = t.length();
    int j = 0;  t.insert(0, " ");

    f(i, 2, m+1)
    {
        while(j > 0 && t[j+1] != t[i]) j = pre[j];
        if(t[j+1] == t[i]) j++;
        pre[i] = j;
    }

    dfs(1, -1, 0);
    cout<<ans;
}


#include<bits/stdc++.h>
#define pb push_back
//#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define openfile ifstream cin; ofstream cout; cin.open("input.txt"); cout.open("output.txt");
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define ll long long
#define key pair<int, int>
#define keyd pair<double, double>
#define ff first
#define ss second
#define double long double
int mod = 1e9+7;
const int inf = 1e18;
using namespace std;

const int size = 300001;
string t, s[size+1];
int m, pre[size+1], ans = 0;;
vector<int> g[size+1];

void dfs(int sr, int p, int j)
{
    int n = s[sr].length(); s[sr].insert(0, " ");
    f(i, 1, n+1)
    {
        while(j > 0 && t[j+1] != s[sr][i]) j = pre[j];
        if(t[j+1] == s[sr][i]) j++;
        if(j == m)
        {
            ans++; j = pre[m];
        }
    }
    for(int i : g[sr])
        if(i != p)
            dfs(i, sr, j);
}

main()
{
    fastread;
    int n; cin>>n;
    f(i, 2, n+1)
    {
        int p; cin>>p>>s[i];
        g[i].pb(p); g[p].pb(i);
    }
    cin>>t;
    pre[1] = 0; m = t.length();
    int j = 0;  t.insert(0, " ");

    f(i, 2, m+1)
    {
        while(j > 0 && t[j+1] != t[i]) j = pre[j];
        if(t[j+1] == t[i]) j++;
        pre[i] = j;
    }

    dfs(1, -1, 0);
    cout<<ans;
}


#include<bits/stdc++.h>
#define pb push_back
//#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define openfile ifstream cin; ofstream cout; cin.open("input.txt"); cout.open("output.txt");
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define ll long long
#define key pair<int, int>
#define keyd pair<double, double>
#define ff first
#define ss second
#define double long double
int mod = 1e9+7;
const int inf = 1e18;
using namespace std;

const int size = 300001;
string t, s[size+1];
int m, pre[size+1], ans = 0;;
vector<int> g[size+1];

int find(string s, int j)
{
    int n = s.length(); s.insert(0, " ");
    f(i, 1, n+1)
    {
        while(j > 0 && t[j+1] != s[i]) j = pre[j];
        if(t[j+1] == s[i]) j++;
        if(j == m)
        {
            ans++; j = pre[m];
        }
    }
    return j;
}

void dfs(int sr, int p, int j)
{
    map<string, int> m;
    for(int i : g[sr])
        if(i != p)
            m[s[i]]++;
    for(auto it = m.begin(); it != m.end(); it++)
        (*it).ss = find((*it).ff, j);

    for(int i : g[sr])
        if(i != p)
            dfs(i, sr, m[s[i]]);
}

main()
{
    fastread;
    int n; cin>>n;
    f(i, 2, n+1)
    {
        int p; cin>>p>>s[i];
        g[i].pb(p); g[p].pb(i);
    }
    cin>>t;
    pre[1] = 0; m = t.length();
    int j = 0;  t.insert(0, " ");

    f(i, 2, m+1)
    {
        while(j > 0 && t[j+1] != t[i]) j = pre[j];
        if(t[j+1] == t[i]) j++;
        pre[i] = j;
    }

    dfs(1, -1, 0);
    cout<<ans;
}


#include<bits/stdc++.h>
#define pb push_back
//#define mp make_pair
#define fastread ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define openfile ifstream cin; ofstream cout; cin.open("input.txt"); cout.open("output.txt");
#define f(i, x, y) for(int i = x; i < y; i++)
#define all(X) X.begin(), X.end()
#define int long long
#define ll long long
#define key pair<int, int>
#define keyd pair<double, double>
#define ff first
#define ss second
#define double long double
int mod = 1e9+7;
const int inf = 1e18;
using namespace std;

const int size = 300001;
string t, s[size+1];
int m, pre[size+1], ans = 0;;
vector<int> g[size+1];

int find(string s, int j, int cnt)
{
    int n = s.length(); s.insert(0, " ");
    int x = 0;
    f(i, 1, n+1)
    {
        while(j > 0 && t[j+1] != s[i]) j = pre[j];
        if(t[j+1] == s[i]) j++;
        if(j == m)
        {
            x++; j = pre[m];
        }
    }
    ans += x*cnt;
    return j;
}

void dfs(int sr, int p, int j)
{
    map<string, int> m;
    for(int i : g[sr])
        if(i != p)
            m[s[i]]++;
    for(auto it = m.begin(); it != m.end(); it++)
        (*it).ss = find((*it).ff, j, (*it).ss);

    for(int i : g[sr])
        if(i != p)
            dfs(i, sr, m[s[i]]);
}

main()
{
    fastread;
    int n; cin>>n;
    f(i, 2, n+1)
    {
        int p; cin>>p>>s[i];
        g[i].pb(p); g[p].pb(i);
    }
    cin>>t;
    pre[1] = 0; m = t.length();
    int j = 0;  t.insert(0, " ");

    f(i, 2, m+1)
    {
        while(j > 0 && t[j+1] != t[i]) j = pre[j];
        if(t[j+1] == t[i]) j++;
        pre[i] = j;
    }

    dfs(1, -1, 0);
    cout<<ans;
}


//In the name of God B-)	:Alikhm
#include <bits/stdc++.h>
#define pb push_back
#define po pop_back
#define ins insert
#define co continue
#define db(a,b,c)	cout<<a<<" "<<b<<" "<<c<<endl
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const LL MAXN=1e5+1,INF=1e17,MOD=1e9+9,b=37;

vector <LL> G[MAXN];
vector <char> c;

LL ans,n,k,pt[300000+1],H,h[300000+1],p;
string s[MAXN],t;
bool mark[MAXN];

LL HASH(string a){
    LL S=0;
    for(int i=0;i<a.size();++i) S=(S+pt[i]*a[i])%MOD;
    return S;
}
LL clc(LL a,LL b){return (a-b+MOD)%MOD;}
void dfs(int u=1){
//    cout<<u<<" "<<G[u].size()<<endl;
    mark[u]=1;
    for(int i=0;i<G[u].size();++i){
        int v=G[u][i],o=s[v].size();
        if(!mark[v]){
            int ll=0;
            if(c.size()==0){ll=1;h[0]=s[v][0];c.pb(s[v][0]);++p;}
            //cout<<"********************************"<<endl;
            //db(v,l,h[0]);
            //cout<<c[v].size()<<" "<<s[v].size()<<endl;
            //cout<<v<<"*"<<endl;
            for(int j=ll;j<s[v].size();++j){
                c.pb(s[v][j]);
                h[p]=(h[p-1]+pt[p]*s[v][j])%MOD;
                //db(h[p],p,s[v][j]);
                //db(h[p],p,s[v][j]);
                //if(p>k)cout<<clc(h[p],h[p-k])<<"*"<<endl;
                if(p>=k&&(clc(h[p],h[p-k])==(H*pt[p-k+1])%MOD)){++ans;}
                if(p==k-1&&h[p]==H){++ans;}
                ++p;
            }
            dfs(v);
            for(int j=0;j<s[v].size();++j)c.po();
            p-=int(s[v].size());
        }
    }
}

int main()
{
	ios_base::sync_with_stdio(0);
	pt[0]=1;
	cin>>n;
	for(int i=1;i<=300000+1;++i){pt[i]=(pt[i-1]*b)%MOD;}
	for(int i=1;i<=n-1;++i){
        LL a;
        cin>>a>>s[i+1];
        G[a].pb(i+1);
        G[i+1].pb(a);
	}
	cin>>t;
	k=t.size();
	H=HASH(t);
	//cout<<H<<"***"<<endl;
	dfs();
	//for(int i=0;i<c[5].size();++i)  cout<<c[5][i]<<endl;
	cout<<ans<<endl;
	return 0;
}


//In the name of God B-)	:Alikhm
#include <bits/stdc++.h>
#define pb push_back
#define po pop_back
#define ins insert
#define co continue
#define db(a,b,c)	cout<<a<<" "<<b<<" "<<c<<endl
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const LL MAXN=1e5+1,INF=1e17,MOD=1e9+9,b=37;

vector <LL> G[MAXN];
vector <char> c[MAXN];

LL ans,n,k,pt[300000+1],H;
string s[MAXN],t;
bool mark[MAXN];

LL HASH(string a){
    LL S=0;
    for(int i=0;i<a.size();++i) S=(S+pt[i]*a[i])%MOD;
    return S;
}
LL clc(LL a,LL b){return (a-b+MOD)%MOD;}
void dfs(int u=1){
//    cout<<u<<" "<<G[u].size()<<endl;
    mark[u]=1;
    for(int i=0;i<G[u].size();++i){
        int v=G[u][i],o=s[v].size();
        LL h[o+k+1];
        if(!mark[v]){
            int ll=0,p=1,l=c[u].size(),r;
            r= k<=l   ?   k-1 :l-2;
            if(l){
                c[v].pb(c[u][l-2-r]);
                h[0]=c[u][l-2-r];
            }else{h[0]=s[v][0];c[v].pb(s[v][0]);ll=1;}
            //cout<<"********************************"<<endl;
            //db(v,l,h[0]);
            for(int j=l-r-1;j<c[u].size();++j){

                c[v].pb(c[u][j]);
                h[p]=(h[p-1]+pt[p]*c[u][j])%MOD;
                //cout<<h[p]<<" "<<p<<" "<<c[u][j]<<endl;
                ++p;
            }
            //cout<<c[v].size()<<" "<<s[v].size()<<endl;
            for(int j=ll;j<s[v].size();++j){
                c[v].pb(s[v][j]);
                h[p]=(h[p-1]+pt[p]*s[v][j])%MOD;
                //db(h[p],p,s[v][j]);
                //if(p>k)cout<<clc(h[p],h[p-k])<<"*"<<endl;
                if(p>=k&&(clc(h[p],h[p-k])==(H*pt[p-k+1])%MOD)){++ans;}
                if(p==k-1&&h[p]==H){++ans;}
                ++p;
            }
            dfs(v);
        }
    }
}

int main()
{
	ios_base::sync_with_stdio(0);
	pt[0]=1;
	cin>>n;
	for(int i=1;i<=300000+1;++i){pt[i]=(pt[i-1]*b)%MOD;}
	for(int i=1;i<=n-1;++i){
        LL a;
        cin>>a>>s[i+1];
        G[a].pb(i+1);
        G[i+1].pb(a);
	}
	cin>>t;
	k=t.size();
	H=HASH(t);
	//cout<<H<<"***"<<endl;
	dfs();
	//for(int i=0;i<c[5].size();++i)  cout<<c[5][i]<<endl;
	cout<<ans<<endl;
	return 0;
}


//In the name of God B-)	:Alikhm
#include <bits/stdc++.h>
#define pb push_back
#define po pop_back
#define ins insert
#define co continue
#define db(a,b,c)	cout<<a<<" "<<b<<" "<<c<<endl
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const LL MAXN=1e5+1,INF=1e17,MOD=1e9+9,b=37;

vector <LL> G[MAXN];
vector <char> c[MAXN];

LL ans,n,k,pt[300000+1],H,h[300000+1];
string s[MAXN],t;
bool mark[MAXN];

LL HASH(string a){
    LL S=0;
    for(int i=0;i<a.size();++i) S=(S+pt[i]*a[i])%MOD;
    return S;
}
LL clc(LL a,LL b){return (a-b+MOD)%MOD;}
void dfs(int u=1){
//    cout<<u<<" "<<G[u].size()<<endl;
    mark[u]=1;
    for(int i=0;i<G[u].size();++i){
        int v=G[u][i],o=s[v].size();
        if(!mark[v]){
            int ll=0,p=1,l=c[u].size(),r;
            r= k<=l   ?   k-1 :l-2;
            if(l){
                c[v].pb(c[u][l-2-r]);
                h[0]=c[u][l-2-r];
            }else{h[0]=s[v][0];c[v].pb(s[v][0]);ll=1;}
            //cout<<"********************************"<<endl;
            //db(v,l,h[0]);
            for(int j=l-r-1;j<c[u].size();++j){

                c[v].pb(c[u][j]);
                h[p]=(h[p-1]+pt[p]*c[u][j])%MOD;
                //cout<<h[p]<<" "<<p<<" "<<c[u][j]<<endl;
                ++p;
            }
            //cout<<c[v].size()<<" "<<s[v].size()<<endl;
            for(int j=ll;j<s[v].size();++j){
                c[v].pb(s[v][j]);
                h[p]=(h[p-1]+pt[p]*s[v][j])%MOD;
                //db(h[p],p,s[v][j]);
                //if(p>k)cout<<clc(h[p],h[p-k])<<"*"<<endl;
                if(p>=k&&(clc(h[p],h[p-k])==(H*pt[p-k+1])%MOD)){++ans;}
                if(p==k-1&&h[p]==H){++ans;}
                ++p;
            }
            dfs(v);
        }
    }
}

int main()
{
	ios_base::sync_with_stdio(0);
	pt[0]=1;
	cin>>n;
	for(int i=1;i<=300000+1;++i){pt[i]=(pt[i-1]*b)%MOD;}
	for(int i=1;i<=n-1;++i){
        LL a;
        cin>>a>>s[i+1];
        G[a].pb(i+1);
        G[i+1].pb(a);
	}
	cin>>t;
	k=t.size();
	H=HASH(t);
	//cout<<H<<"***"<<endl;
	dfs();
	//for(int i=0;i<c[5].size();++i)  cout<<c[5][i]<<endl;
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+50;
int n,lp[N],ans;
string pat;
struct e{int to;string s;};
vector<vector<e> >adj(N);
void dfs(int i,int p)
{
    if(p==pat.size())return;
    for(auto u:adj[i])
    {
        int nxt=p;
        for(int j=0;j<u.s.size();j++)
        {
            while(nxt&&pat[nxt]!=u.s[j])nxt=lp[nxt-1];
            if(pat[nxt]==u.s[j])nxt++;
            if(nxt==pat.size())ans++,nxt=lp[nxt-1];
        }
        dfs(u.to,nxt);
    }
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    int u;
    string x;
    for(int i=2;i<=n;i++)
    {
        cin>>u>>x;
        adj[u].push_back({i,x});
    }
    cin>>pat;
    for(int i=1,k=0;i<pat.size();i++)
	{
		while(k>0&&pat[k]!=pat[i])k=lp[k-1];
		if(pat[k]==pat[i])lp[i]=++k;
		else lp[i]=k;
	}
    dfs(1,0);
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+50;
int n,lp[N],ans,dp[26][3*N];
string pat;
struct e{int to;string s;};
vector<vector<e> >adj(N);
void dfs(int i,int p)
{
    if(p==pat.size())return;
    for(auto u:adj[i])
    {
        int nxt=p;
        for(int j=0;j<u.s.size();j++)
        {
            int &x=dp[u.s[j]-'a'][nxt];
            if(x==-1)
            {
                while(nxt&&pat[nxt]!=u.s[j])nxt=lp[nxt-1];
                x=nxt;
            }
            nxt=x;
            if(pat[nxt]==u.s[j])nxt++;
            if(nxt==pat.size())ans++,nxt=lp[nxt-1];
        }
        dfs(u.to,nxt);
    }
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    int u;
    string x;
    for(int i=2;i<=n;i++)
    {
        cin>>u>>x;
        adj[u].push_back({i,x});
    }
    cin>>pat;
    for(int i=1,k=0;i<pat.size();i++)
	{
		while(k>0&&pat[k]!=pat[i])k=lp[k-1];
		if(pat[k]==pat[i])lp[i]=++k;
		else lp[i]=k;
	}
	memset(dp,-1,sizeof dp);
    dfs(1,0);
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+50;
int n,lp[N],ans;
string pat;
struct e{int to;string s;};
vector<vector<e> >adj(N);
void dfs(int i,int p)
{
    if(p==pat.size())return;
    for(auto u:adj[i])
    {
        int nxt=p;
        for(int j=0;j<u.s.size();j++)
        {
            while(nxt&&pat[nxt]!=u.s[j])nxt=lp[nxt-1];
            if(pat[nxt]==u.s[j])nxt++;
            if(nxt==pat.size())ans++,nxt=lp[nxt-1];
        }
        dfs(u.to,nxt);
    }
}
int main()
{
    cin>>n;
    int u;
    string x;
    for(int i=2;i<=n;i++)
    {
        cin>>u>>x;
        adj[u].push_back({i,x});
    }
    cin>>pat;
    for(int i=1,k=0;i<pat.size();i++)
	{
		while(k>0&&pat[k]!=pat[i])k=lp[k-1];
		if(pat[k]==pat[i])lp[i]=++k;
		else lp[i]=k;
	}
    dfs(1,0);
    cout<<ans;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define ull          unsigned long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;
static const ll mod = 1e9 + 123;

int gen_base(const int before, const int after)
{
	auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
	mt19937 mt_rand(seed);
	int base = uniform_int_distribution <int> (before + 1, after)(mt_rand);
	if (base % 2 == 0) base -= 1;
	return base;
}

ll pow1[maxn];
ull pow2[maxn];
int base;

void preCalc()
{
	assert(base < mod);
	pow1[0] = 1;
	pow2[0] = 1;
	for (int i = 1; i < maxn; i++)
	{
		pow1[i] = (1LL * base * pow1[i - 1]) % mod;
		pow2[i] = (1LL * base * pow2[i - 1]);
	}
}

struct Hash
{
      vector <long long> pref1;
      vector <unsigned long long> pref2;
      Hash() {}
      Hash(const string &s) : pref1(s.size() + 1u, 0), pref2(s.size() + 1u, 0)
      {
            assert(base < mod);
            int n = s.size();
            for (int i = 0; i < n; i++)
            {
                  assert(base > s[i]);
                  pref1[i + 1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
                  pref2[i + 1] = pref2[i] + s[i] * pow2[i];
            }
      }
	void add(const char ch)
	{
		int len = pref1.size();
		pref1.push_back(0);
		pref2.push_back(0);
		pref1[len] = (pref1[len - 1] + 1LL * ch * pow1[len - 1]) % mod;
		pref2[len] = pref2[len - 1] + ch * pow2[len - 1];
	}
	void delet()
	{
		pref1.pop_back();
		pref2.pop_back();
	}
      pair <long long, unsigned long long> getHash(const int pos, const int len, const int mxPow = 0)
      {
            long long hash1 = pref1[pos + len] - pref1[pos];
            unsigned long long hash2 = pref2[pos + len] - pref2[pos];
            if (hash1 < 0) hash1 += mod;
            if (mxPow != 0)
            {
                  hash1 = 1LL * hash1 * pow1[mxPow - (pos + len - 1)] % mod;
                  hash2 = hash2 * pow2[mxPow - (pos + len - 1)];
            }
            return make_pair(hash1, hash2);
      }
};

vector < vector <int> > graph;
string str[maxn];
string pat;
int len_pat;
int ans;
int mxPow;
Hash hsh;
Hash hsh_pat;
int sz;

void dfs(int u = 1)
{
	for (int v : graph[u])
	{
		for (char ch : str[v])
		{
			hsh.add(ch);
			sz++;
			if (sz >= len_pat)
			{
				pair <ll, ull> p = hsh.getHash(sz - len_pat, len_pat, mxPow);
				pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
				if (p == q) ++ans;
			}
		}
		dfs(v);
	}
	for (char ch : str[u])
	{
		hsh.delet();
		sz--;
	}

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	base = gen_base(256, mod);
	preCalc();

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		cin >> u >> str[v];
		mxPow += str[v].size();
		graph[u].push_back(v);
	}
	cin >> pat;
	len_pat = pat.size();
	hsh_pat = Hash(pat);
	hsh.pref1.push_back(0);
	hsh.pref2.push_back(0);
	pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
	dfs(1);
	cout << ans;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define ull          unsigned long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;
static const ll mod = 1e9 + 123;

int gen_base(const int before, const int after)
{
	auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
	mt19937 mt_rand(seed);
	int base = uniform_int_distribution <int> (before + 1, after)(mt_rand);
	if (base % 2 == 0) base -= 1;
	return base;
}

ll pow1[maxn];
ull pow2[maxn];
int base;

void preCalc()
{
	assert(base < mod);
	pow1[0] = 1;
	pow2[0] = 1;
	for (int i = 1; i < maxn; i++)
	{
		pow1[i] = (1LL * base * pow1[i - 1]) % mod;
		pow2[i] = (1LL * base * pow2[i - 1]);
	}
}

struct Hash
{
      vector <long long> pref1;
      vector <unsigned long long> pref2;
      Hash() {}
      Hash(const string &s) : pref1(s.size() + 1u, 0), pref2(s.size() + 1u, 0)
      {
            assert(base < mod);
            int n = s.size();
            for (int i = 0; i < n; i++)
            {
                  assert(base > s[i]);
                  pref1[i + 1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
                  pref2[i + 1] = pref2[i] + s[i] * pow2[i];
            }
      }
	void add(const char ch)
	{
		int len = pref1.size();
		pref1.push_back(0);
		pref2.push_back(0);
		pref1[len] = (pref1[len - 1] + 1LL * ch * pow1[len - 1]) % mod;
		pref2[len] = pref2[len - 1] + ch * pow2[len - 1];
	}
	void delet()
	{
		pref1.pop_back();
		pref2.pop_back();
	}
      pair <long long, unsigned long long> getHash(const int pos, const int len, const int mxPow = 0)
      {
            long long hash1 = pref1[pos + len] - pref1[pos];
            unsigned long long hash2 = pref2[pos + len] - pref2[pos];
            if (hash1 < 0) hash1 += mod;
            if (mxPow != 0)
            {
                  hash1 = 1LL * hash1 * pow1[mxPow - (pos + len - 1)] % mod;
                  hash2 = hash2 * pow2[mxPow - (pos + len - 1)];
            }
            return make_pair(hash1, hash2);
      }
};

vector < vector <int> > graph;
string str[maxn];
string pat;
int len_pat;
int ans;
int mxPow;
Hash hsh;
Hash hsh_pat;
int sz;

void dfs(int u = 1)
{
	for (int v : graph[u])
	{
		for (char ch : str[v])
		{
			hsh.add(ch);
			sz++;
			if (sz >= len_pat)
			{
				pair <ll, ull> p = hsh.getHash(sz - len_pat, len_pat, mxPow);
				pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
				if (p == q) ++ans;
			}
		}
		dfs(v);
	}
	for (char ch : str[u])
	{
		hsh.delet();
		sz--;
	}

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	base = gen_base(256, mod);
	preCalc();

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		cin >> u >> str[v];
		mxPow += str[v].size();
		graph[u].push_back(v);
	}
	cin >> pat;
	len_pat = pat.size();
	hsh_pat = Hash(pat);
	hsh.pref1.push_back(0);
	hsh.pref2.push_back(0);
	pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
	dfs(1);
	cout << ans << endl;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;

string pat;
int len_pat;
int lps[maxn];

void longest_prefix_suffix()
{
	int i = 1;
	int j = 0;
	while (i < len_pat)
	{
		if (pat[i] == pat[j])
		{
			lps[i] = j + 1;
			i++;
			j++;
		}
		else
		{
			if (j) j = lps[j - 1];
			else lps[i] = 0, i++;
		}
	}
}

ll ans;

int kmp(char ch, int j)
{
	while (j and ch != pat[j]) j = lps[j - 1];
	if (ch == pat[j]) j++;
	if (j == len_pat) ++ans, j = lps[j - 1];
	return j;
}

string str[maxn];
vector < vector <int> > graph;

void dfs(int u, int j)
{
	for (int v : graph[u])
	{
		int jj = j;
		for (char ch : str[v]) jj = kmp(ch, jj);
		dfs(v, jj);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		cin >> u >> str[v];
		graph[u].push_back(v);
	}
	cin >> pat;
	len_pat = pat.size();
	longest_prefix_suffix();
	dfs(1, 0);
	cout << ans << endl;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define endl         '\n'

const int maxN = 100*1000 + 5;
const int lim = 1000*1000*1000;
const int inf = 1000*1000*1000 + 5;
int f[3 * maxN];
vector<pair<int, string> > a[maxN];
string t;
int ans;
void dfs(int v, int F)
{
    for(auto u : a[v])
    {
	  int tmp = F; string s = u.second;
	  for(int i = 0; i < s.size(); i ++)
	  {
		while(tmp && (tmp == t.size() || t[tmp] != s[i])) tmp = f[tmp - 1];
		if(t[tmp] == s[i]) tmp ++;
		if(tmp == t.size()) ans ++;
	  }
	  dfs(u.first, tmp);
    }
}
main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i < n; i ++)
    {
	  int par; string s; cin >> par >> s; par -- ;
	  a[par].push_back(make_pair(i, s));
    }
    cin >> t;
    int tmp = 0;
    for(int i = 1; i < t.size(); i ++)
    {
	  while(tmp && t[tmp] != t[i]) tmp = f[tmp - 1];
	  if(t[tmp] == t[i]) tmp ++;
	  f[i] = tmp;
    }
    dfs(0, 0);
    cout << ans << endl;
    return 0;

}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define endl         '\n'

const int maxN = 100*1000 + 5;
const int lim = 1000*1000*1000;
const int inf = 1000*1000*1000 + 5;
int f[3 * maxN];
vector<pair<int, string> > a[maxN];
string t;
int ans;
void dfs(int v, int F)
{
    for(auto u : a[v])
    {
	  int tmp = F; string s = u.second;
	  for(int i = 0; i < s.size(); i ++)
	  {
		while(tmp && (tmp == t.size() || t[tmp] != s[i])) tmp = f[tmp - 1];
		if(t[tmp] == s[i]) tmp ++;
		if(tmp == t.size()) ans ++;
	  }
	  dfs(u.first, tmp);
    }
}
main()
{
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i < n; i ++)
    {
	  int par; string s; cin >> par >> s; par -- ;
	  a[par].push_back(make_pair(i, s));
    }
    cin >> t;
    int tmp = 0;
    for(int i = 1; i < t.size(); i ++)
    {
	  while(tmp && t[tmp] != t[i]) tmp = f[tmp - 1];
	  if(t[tmp] == t[i]) tmp ++;
	  f[i] = tmp;
    }
    dfs(0, 0);
    cout << ans << endl;

}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;

string pat;
int len_pat;
int lps[maxn];

void longest_prefix_suffix()
{
	int i = 1;
	int j = 0;
	while (i < len_pat)
	{
		if (pat[i] == pat[j])
		{
			lps[i] = j + 1;
			i++;
			j++;
		}
		else
		{
			if (j) j = lps[j - 1];
			else lps[i] = 0, i++;
		}
	}
}

ll ans;

int kmp(char ch, int j)
{
	while (j and ch != pat[j]) j = lps[j - 1];
	if (ch == pat[j]) j++;
	if (j == len_pat) ++ans, j = lps[j - 1];
	return j;
}

vector < vector < pair <int, string> > > graph;

void dfs(int u, int j)
{
	for (auto it : graph[u])
	{
		int v = it.first;
		string s = it.second;
		int jj = j;
		for (char ch : s)
		{
			while (jj and (jj == len_pat or pat[jj] != ch)) jj = lps[jj - 1];
			if (ch == pat[jj]) jj++;
			if (jj == len_pat) ++ans;
		}
		dfs(v, jj);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	// cout.tie(nullptr);

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		string s;
		cin >> u >> s;
		graph[u].push_back(make_pair(v, s));
	}
	cin >> pat;
	len_pat = pat.size();
	int j = 0;
	for (int i = 1; i < len_pat; i++)
	{
		while (j and pat[i] != pat[j]) j = pat[j - 1];
		if (pat[i] == pat[j]) j++;
		lps[i] = j;
	}
	dfs(1, 0);
	cout << ans << endl;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define endl         '\n'

static const int maxn = 2e5 + 5;

string pat;
int len_pat;
int lps[maxn];

void longest_prefix_suffix()
{
	int i = 1;
	int j = 0;
	while (i < len_pat)
	{
		if (pat[i] == pat[j])
		{
			lps[i] = j + 1;
			i++;
			j++;
		}
		else
		{
			if (j) j = lps[j - 1];
			else lps[i] = 0, i++;
		}
	}
}

ll ans;

int kmp(char ch, int j)
{
	while (j and ch != pat[j]) j = lps[j - 1];
	if (ch == pat[j]) j++;
	if (j == len_pat) ++ans, j = lps[j - 1];
	return j;
}

string str[maxn];
vector < vector <int> > graph;

void dfs(int u, int j)
{
	for (int v : graph[u])
	{
		int jj = j;
		for (char ch : str[v]) jj = kmp(ch, jj);
		dfs(v, jj);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		cin >> u >> str[v];
		graph[u].push_back(v);
	}
	cin >> pat;
	len_pat = pat.size();
	longest_prefix_suffix();
	dfs(1, 0);
	cout << ans << endl;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;

string pat;
int len_pat;
int lps[maxn];

void longest_prefix_suffix()
{
	int i = 1;
	int j = 0;
	while (i < len_pat)
	{
		if (pat[i] == pat[j])
		{
			lps[i] = j + 1;
			i++;
			j++;
		}
		else
		{
			if (j) j = lps[j - 1];
			else lps[i] = 0, i++;
		}
	}
}

ll ans;

int kmp(char ch, int j)
{
	while (j and ch != pat[j]) j = lps[j - 1];
	if (ch == pat[j]) j++;
	if (j == len_pat) ++ans, j = lps[j - 1];
	return j;
}

string str[maxn];
vector < vector < pair <int, string> > > graph;

void dfs(int u, int j)
{
	for (auto it : graph[u])
	{
		int v = it.first;
		string s = it.second;
		int jj = j;
		for (char ch : s)
		{
			while (jj and (jj == len_pat or pat[jj] != ch)) jj = lps[jj - 1];
			if (ch == pat[jj]) jj++;
			if (jj == len_pat) ++ans;
		}
		dfs(v, jj);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	// cout.tie(nullptr);

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		string s;
		cin >> u >> s;
		graph[u].push_back(make_pair(v, s));
	}
	cin >> pat;
	len_pat = pat.size();
	longest_prefix_suffix();
	dfs(1, 0);
	cout << ans << endl;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define ull          unsigned long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;
static const ll mod = 1e9 + 123;

int gen_base(const int before, const int after)
{
	auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
	mt19937 mt_rand(seed);
	int base = uniform_int_distribution <int> (before + 1, after)(mt_rand);
	if (base % 2 == 0) base -= 1;
	return base;
}

ll pow1[maxn];
ull pow2[maxn];
int base;

void preCalc()
{
	assert(base < mod);
	pow1[0] = 1;
	pow2[0] = 1;
	for (int i = 1; i < maxn; i++)
	{
		pow1[i] = (1LL * base * pow1[i - 1]) % mod;
		pow2[i] = (1LL * base * pow2[i - 1]);
	}
}

struct Hash
{
      vector <long long> pref1;
      vector <unsigned long long> pref2;
      Hash() {}
      Hash(const string &s) : pref1(s.size() + 1u, 0), pref2(s.size() + 1u, 0)
      {
            assert(base < mod);
            int n = s.size();
            for (int i = 0; i < n; i++)
            {
                  assert(base > s[i]);
                  pref1[i + 1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
                  pref2[i + 1] = pref2[i] + s[i] * pow2[i];
            }
      }
	void add(const char ch)
	{
		int len = pref1.size();
		pref1.push_back(0);
		pref2.push_back(0);
		pref1[len] = (pref1[len - 1] + 1LL * ch * pow1[len - 1]) % mod;
		pref2[len] = pref2[len - 1] + ch * pow2[len - 1];
	}
	void delet()
	{
		pref1.pop_back();
		pref2.pop_back();
	}
      pair <long long, unsigned long long> getHash(const int pos, const int len, const int mxPow = 0)
      {
            long long hash1 = pref1[pos + len] - pref1[pos];
            unsigned long long hash2 = pref2[pos + len] - pref2[pos];
            if (hash1 < 0) hash1 += mod;
            if (mxPow != 0)
            {
                  hash1 = 1LL * hash1 * pow1[mxPow - (pos + len - 1)] % mod;
                  hash2 = hash2 * pow2[mxPow - (pos + len - 1)];
            }
            return make_pair(hash1, hash2);
      }
};

vector < vector <int> > graph;
string str[maxn];
string pat;
int len_pat;
int ans;
int mxPow;
Hash hsh;
Hash hsh_pat;
int sz;

void dfs(int u = 1)
{
	for (int v : graph[u])
	{
		//cerr << "v=" << v << ", str[v]=" << str[v] << endl;
		for (char ch : str[v])
		{
			hsh.add(ch);
			sz++;
			if (sz >= len_pat)
			{
				pair <ll, ull> p = hsh.getHash(sz - len_pat, len_pat, mxPow);
				pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
				//cerr << p.first << ", " << p.second << endl;
				//cerr << q.first << ", " << q.second << endl;
				if (p == q) ++ans;
			}
		}
		dfs(v);
	}
	for (char ch : str[u])
	{
		hsh.delet();
		sz--;
	}

}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	base = gen_base(256, mod);
	preCalc();

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		cin >> u >> str[v];
		mxPow += str[v].size();
		graph[u].push_back(v);
	}
	cin >> pat;
	len_pat = pat.size();
	hsh_pat = Hash(pat);
	hsh.pref1.push_back(0);
	hsh.pref2.push_back(0);
	pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
	//cerr << q.first << " " << q.second << endl;
	dfs(1);
	cout << ans << endl;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;

string pat;
int len_pat;
int lps[maxn];

void longest_prefix_suffix()
{
	int i = 1;
	int j = 0;
	while (i < len_pat)
	{
		if (pat[i] == pat[j])
		{
			lps[i] = j + 1;
			i++;
			j++;
		}
		else
		{
			if (j) j = lps[j - 1];
			else lps[i] = 0, i++;
		}
	}
}

ll ans;

int kmp(char ch, int j)
{
	while (j and ch != pat[j]) j = lps[j - 1];
	if (ch == pat[j]) j++;
	if (j == len_pat) ++ans, j = lps[j - 1];
	return j;
}

string str[maxn];
vector < vector <int> > graph;

void dfs(int u, int j)
{
	for (int v : graph[u])
	{
		int jj = j;
		for (char ch : str[v]) jj = kmp(ch, jj);
		dfs(v, jj);
	}
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	// cout.tie(nullptr);

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		cin >> u >> str[v];
		graph[u].push_back(v);
	}
	cin >> pat;
	len_pat = pat.size();
	longest_prefix_suffix();
	dfs(1, 0);
	cout << ans << endl;
}


#include "bits/stdc++.h"

using namespace std;

#define ll           long long int
#define ull          unsigned long long int
#define endl         '\n'

static const int maxn = 4e5 + 5;
static const ll mod = 1e9 + 123;

int gen_base(const int before, const int after)
{
	auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
	mt19937 mt_rand(seed);
	int base = uniform_int_distribution <int> (before + 1, after)(mt_rand);
	if (base % 2 == 0) base -= 1;
	return base;
}

ll pow1[maxn];
ull pow2[maxn];
int base;

void preCalc()
{
	assert(base < mod);
	pow1[0] = 1;
	pow2[0] = 1;
	for (int i = 1; i < maxn; i++)
	{
		pow1[i] = (1LL * base * pow1[i - 1]) % mod;
		pow2[i] = (1LL * base * pow2[i - 1]);
	}
}

struct Hash
{
      vector <long long> pref1;
      vector <unsigned long long> pref2;
      Hash() {}
      Hash(const string &s) : pref1(s.size() + 1u, 0), pref2(s.size() + 1u, 0)
      {
            assert(base < mod);
            int n = s.size();
            for (int i = 0; i < n; i++)
            {
                  assert(base > s[i]);
                  pref1[i + 1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
                  pref2[i + 1] = pref2[i] + s[i] * pow2[i];
            }
      }
	void add(const char ch)
	{
		int len = pref1.size();
		pref1.push_back(0);
		pref2.push_back(0);
		pref1[len] = (pref1[len - 1] + 1LL * ch * pow1[len - 1]) % mod;
		pref2[len] = pref2[len - 1] + ch * pow2[len - 1];
	}
	void delet()
	{
		pref1.pop_back();
		pref2.pop_back();
	}
      pair <long long, unsigned long long> getHash(const int pos, const int len, const int mxPow = 0)
      {
            long long hash1 = pref1[pos + len] - pref1[pos];
            unsigned long long hash2 = pref2[pos + len] - pref2[pos];
            if (hash1 < 0) hash1 += mod;
            if (mxPow != 0)
            {
                  hash1 = 1LL * hash1 * pow1[mxPow - (pos + len - 1)] % mod;
                  hash2 = hash2 * pow2[mxPow - (pos + len - 1)];
            }
            return make_pair(hash1, hash2);
      }
};

vector < vector <int> > graph;
string str[maxn];
string pat;
int len_pat;
int ans;
int mxPow;
Hash hsh;
Hash hsh_pat;
int sz;

void dfs(int u = 1)
{
	for (int v : graph[u])
	{
		for (char ch : str[v])
		{
			hsh.add(ch);
			sz++;
			if (sz >= len_pat)
			{
				pair <ll, ull> p = hsh.getHash(sz - len_pat, len_pat, mxPow);
				pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
				if (p == q) ++ans;
			}
		}
		dfs(v);
	}
	for (char ch : str[u])
	{
		hsh.delet();
		sz--;
	}

}

signed main()
{
	base = gen_base(256, mod);
	preCalc();

	int n;
	cin >> n;
	graph.resize(n + 1);
	for (int v = 2; v <= n; v++)
	{
		int u;
		cin >> u >> str[v];
		mxPow += str[v].size();
		graph[u].push_back(v);
	}
	cin >> pat;
	len_pat = pat.size();
	hsh_pat = Hash(pat);
	hsh.pref1.push_back(0);
	hsh.pref2.push_back(0);
	pair <ll, ull> q = hsh_pat.getHash(0, len_pat, mxPow);
	dfs(1);
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
vector <pair <int, string>> child[MAXN];
struct node {
    vector <pair <node * , char>> child;
} * pointer[MAXN];
node * add(node * v, string & s) {
    if (s.empty())
        return v;
    node * next = new node;
    v -> child.push_back({next, s.back()});
    s.pop_back();
    return add(next, s);
}
void build(int v) {
    for (auto i : child[v]) {
        reverse(i.second.begin(), i.second.end());
        pointer[i.first] = add(pointer[v], i.second);
        build(i.first);
    }
}
string t;
int ts;
int f[MAXN], cnt;
void dfs(node * v, char now, int p) {
    while(~p && t[p] != now)
        p = (p ? f[p - 1] : -1);
    ++p;
    cnt += p == ts;
    for (auto i : v -> child)
        dfs(i.first, i.second, p);
}
int main () {
    pointer[1] = new node;
    int n, a1;
    string a2;
    cin >> n;
    for (int i = 2; i <= n; ++i) {
        cin >> a1 >> a2;
        child[a1].push_back({i, a2});
    }
    build(1);
    cin >> t;
    ts = t.size();
    t += "!";
    int p = f[0] = 0;
    for (int i = 1; i <= ts; ++i) {
        while(~p && t[p] != t[i])
            p = (p ? f[p - 1] : -1);
        f[i] = ++p;
    }
    dfs(pointer[1], '?', 0);
    cout << cnt;
    return 0;
}