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

namespace Test
{
    class IOHelper : IDisposable
    {
        StreamReader reader;
        StreamWriter writer;

        public IOHelper(string inputFile, string outputFile, Encoding encoding)
        {
            StreamReader iReader;
            StreamWriter oWriter;
            if (inputFile == null)
                iReader = new StreamReader(Console.OpenStandardInput(), encoding);
            else
                iReader = new StreamReader(inputFile, encoding);

            if (outputFile == null)
                oWriter = new StreamWriter(Console.OpenStandardOutput(), encoding);
            else
                oWriter = new StreamWriter(outputFile, false, encoding);

            reader = iReader;
            writer = oWriter;

            curLine = new string[] { };
            curTokenIdx = 0;
        }

        string[] curLine;
        int curTokenIdx;

        char[] whiteSpaces = new char[] { ' ', '\t', '\r', '\n' };

        public string ReadNextToken()
        {
            if (curTokenIdx >= curLine.Length)
            {
                //Read next line
                string line = reader.ReadLine();
                if (line != null)
                    curLine = line.Split(whiteSpaces, StringSplitOptions.RemoveEmptyEntries);
                else
                    curLine = new string[] { };
                curTokenIdx = 0;
            }

            if (curTokenIdx >= curLine.Length)
                return null;

            return curLine[curTokenIdx++];
        }

        public int ReadNextInt()
        {
            return int.Parse(ReadNextToken());
        }

        public double ReadNextDouble()
        {
            var nextToken = ReadNextToken();
            var result = 0.0;
            nextToken = nextToken.Replace(".", System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
            result = double.Parse(nextToken);
            return result;
        }

        public void Write(string stringToWrite)
        {
            writer.Write(stringToWrite);
        }

        public void WriteLine(string stringToWrite)
        {
            writer.WriteLine(stringToWrite);
        }

        public void WriteLine(double valueToWrite)
        {
			writer.WriteLine(valueToWrite.ToString("F8"));
        }

        public void Dispose()
        {
            try
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                if (writer != null)
                {
                    writer.Dispose();
                }
            }
            catch { };
        }

        public void Flush()
        {
            if (writer != null)
            {
                writer.Flush();
            }
        }
    }

    class Program
    {
        protected IOHelper ioHelper;

        int n, m, r;

        int buy;
        int sell;

        public void Solve()
        {
            n = ioHelper.ReadNextInt();
            m = ioHelper.ReadNextInt();
            r = ioHelper.ReadNextInt();

            buy = int.MaxValue;

            int i;

            for(i=0;i<n;i++)
            {
                int b = ioHelper.ReadNextInt();
                if (b < buy)
                    buy = b;
            }

            sell = 0;

            for (i = 0; i < n; i++)
            {
                int s = ioHelper.ReadNextInt();
                if (s > sell)
                    sell = s;
            }

            int qty = r / buy;

            int result = r - qty * buy;

            result += qty*Math.Max(buy, sell);

            ioHelper.WriteLine(result.ToString());
            ioHelper.Flush();

            //Console.ReadKey();
        }

        public Program(string inputFile, string outputFile)
        {
            ioHelper = new IOHelper(inputFile, outputFile, Encoding.Default);
            Solve();
            ioHelper.Dispose();
        }

        static void Main(string[] args)
        {
            Program myProgram = new Program(null, null);
        }
    }
}


// Problem: 1150A - Stock Arbitraging
// Author: Gusztav Szmolik

using System;

class StockArbitraging
    {
    public static int Main ()
        {
        string line = Console.ReadLine ();
        if (line == null)
            return -1;
        char[] delims = {' ', '\t'};
        string[] words = line.Split (delims,StringSplitOptions.RemoveEmptyEntries);
        if (words.Length != 3)
            return -1;
        byte n;
        if (!Byte.TryParse (words[0], out n))
            return -1;
        if (n < 1 || n > 30)
            return -1;
        byte m;
        if (!Byte.TryParse (words[1], out m))
            return -1;
        if (m < 1 || m > 30)
            return -1;
        ushort r;
        if (!UInt16.TryParse (words[2], out r))
            return -1;
        if (r < 1 || r > 1000)
            return -1;
        line = Console.ReadLine ();
        if (line == null)
            return -1;
        words = line.Split (delims,StringSplitOptions.RemoveEmptyEntries);
        if (words.Length != n)
            return -1;
        ushort sMin = 1000;
        for (byte i = 0; i < n; i++)
            {
            ushort si;
            if (!UInt16.TryParse (words[i], out si))
                return -1;
            if (si < 1 || si > 1000)
                return -1;
            if (si < sMin)
                sMin = si;
            }
        ushort numShare = Convert.ToUInt16 (r/sMin);
        ushort sumBuy = Convert.ToUInt16 (numShare*sMin);
        line = Console.ReadLine ();
        if (line == null)
            return -1;
        words = line.Split (delims,StringSplitOptions.RemoveEmptyEntries);
        if (words.Length != m)
            return -1;
        ushort bMax = 1;
        for (byte i = 0; i < m; i++)
            {
            ushort bi;
            if (!UInt16.TryParse (words[i], out bi))
                return -1;
            if (bi < 1 || bi > 1000)
                return -1;
            if (bi > bMax)
                bMax = bi;
            }
        uint sumSell = Convert.ToUInt16 (numShare*bMax);
        uint ans = (sumSell > sumBuy ? r+sumSell-sumBuy : r);
        Console.WriteLine (ans);
        return 0;
        }
    }


using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
class Problem
{
    static void Main()
    {
        //--------------------------------input--------------------------------//

        var inp = Array.ConvertAll( Console.ReadLine().Split(), Convert.ToInt32 );
        int n = inp[0], m = inp[1], r = inp[2];
        var s = Array.ConvertAll( Console.ReadLine().Split(), Convert.ToInt32 );
        var b = Array.ConvertAll( Console.ReadLine().Split(), Convert.ToInt32 );
        Array.Sort( s );
        Array.Sort( b, (x, y) => y - x );

        //--------------------------------solve--------------------------------/

        int min = Math.Min( s.Length, b.Length );
        int max = r;
        int i=0;
        while(min-- > 0)
        {
            int now = (r / s[i] )* b[i];
            r = r % s[i] + now;
            max = Math.Max( r, max );
            i++;
        }
        Console.WriteLine( max);

    }

}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static bool[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = ReadLongs();
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new bool[m + 1, n + 1, 101];
            prev = new int[m + 1, n + 1, 101];
            value = new long[m + 1, n + 1, 101];
            long res = 0;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = true;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (j == 0 || dp[i, j, u]) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii]) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = true;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii]) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = true;
                                        //if(ii==3) Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u]) {
                        find = true;
                        res = Math.Max(res, a[i] + u);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] && res == a[i] + u) {
                        idx.Add(i + 1);
                        val.Add(res);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add(i + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static bool[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = new long[4];
                var ys = ReadLongs();
                for (int j = 0; j < 3; j++)
                    vvv[i][j] = ys[j];
                vvv[i][3] = i;
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new bool[m, n + 1, 101];
            prev = new int[m, n + 1, 101];
            value = new long[m, n + 1, 101];
            long res = 0;

            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, 0, u] = true;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = true;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (dp[i, j, u]) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii]) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = true;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii]) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = true;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u]) {
                        find = true;
                        res = Math.Max(res, a[i] + u);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] && res == a[i] + u) {
                        idx.Add((int)vvv[i][3] + 1);
                        val.Add(res);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add((int)vvv[i][3] + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static long[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = new long[4];
                var ys = ReadLongs();
                for (int j = 0; j < 3; j++)
                    vvv[i][j] = ys[j];
                vvv[i][3] = i;
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new long[m, n + 1, 101];
            prev = new int[m, n + 1, 101];
            value = new long[m, n + 1, 101];
            long res = 0;

            for (int i = 0; i < m; i++)
                for (int j = 0; j <= n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++)
                        dp[i, j, u] = -1;

            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, 1, u] = a[0] + u;

            for (int i = 0; i < m; i++)
                for (int j = 1; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (dp[i, j, u] >= 0) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            long nv1 = v + dp[i, j, u];
                            long nv2 = vv + dp[i, j, u];
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii] && dp[ii, j + 1, v - a[ii]] < nv1) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = nv1;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii] && dp[ii, j + 1, vv - a[ii]] < nv2) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = nv2;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] >= 0) {
                        find = true;
                        res = Math.Max(res, dp[i, n, u]);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] == res) {
                        idx.Add((int)vvv[i][3] + 1);
                        val.Add(a[i] + u);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add((int)vvv[i][3] + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static bool[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                var ys = ReadLongs();
                a[i] = ys[0];
                b[i] = ys[1];
                c[i] = ys[2];
            }

            dp = new bool[m + 1, n + 1, 101];
            prev = new int[m + 1, n + 1, 101];
            value = new long[m + 1, n + 1, 101];
            long res = 0;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = true;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (j == 0 || dp[i, j, u]) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii]) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = true;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii]) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = true;
                                        //if(ii==3) Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u]) {
                        find = true;
                        res = Math.Max(res, a[i] + u);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] && res == a[i] + u) {
                        idx.Add(i + 1);
                        val.Add(res);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add(i + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static long[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = new long[4];
                var ys = ReadLongs();
                for (int j = 0; j < 3; j++)
                    vvv[i][j] = ys[j];
                vvv[i][3] = i;
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new long[m, n + 1, 101];
            prev = new int[m, n + 1, 101];
            value = new long[m, n + 1, 101];
            long res = 0;

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, j, u] = -1;

            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, 0, u] = 0;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = a[0] + i;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (dp[i, j, u] >= 0) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            long newval = v + dp[i, j, u];
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii] && dp[ii, j + 1, v - a[ii]] < newval) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = newval;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii] && dp[ii, j + 1, vv - a[ii]] < newval) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = newval;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] >= 0) {
                        find = true;
                        res = Math.Max(res, dp[i, n, u]);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] == res) {
                        idx.Add((int)vvv[i][3] + 1);
                        val.Add(a[i] + u);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add((int)vvv[i][3] + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static long[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = new long[4];
                var ys = ReadLongs();
                for (int j = 0; j < 3; j++)
                    vvv[i][j] = ys[j];
                vvv[i][3] = i;
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new long[m, n + 1, 101];
            prev = new int[m, n + 1, 101];
            value = new long[m, n + 1, 101];
            long res = 0;

            for (int i = 0; i < m; i++)
                for (int j = 0; j <= n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++)
                        dp[i, j, u] = -1;

            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, 0, u] = 0;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = a[0] + i;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (dp[i, j, u] >= 0) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            long nv1 = v + dp[i, j, u];
                            long nv2 = vv + dp[i, j, u];
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii] && dp[ii, j + 1, v - a[ii]] < nv1) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = nv1;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii] && dp[ii, j + 1, vv - a[ii]] < nv2) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = nv2;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] >= 0) {
                        find = true;
                        res = Math.Max(res, dp[i, n, u]);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] == res) {
                        idx.Add((int)vvv[i][3] + 1);
                        val.Add(a[i] + u);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add((int)vvv[i][3] + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static bool[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = new long[4];
                var ys = ReadLongs();
                for (int j = 0; j < 3; j++)
                    vvv[i][j] = ys[j];
                vvv[i][3] = i;
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new bool[m + 1, n + 1, 101];
            prev = new int[m + 1, n + 1, 101];
            value = new long[m + 1, n + 1, 101];
            long res = 0;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = true;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (j == 0 || dp[i, j, u]) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii]) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = true;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii]) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = true;
                                        //if(ii==3) Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u]) {
                        find = true;
                        res = Math.Max(res, a[i] + u);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] && res == a[i] + u) {
                        idx.Add((int)vvv[i][3] + 1);
                        val.Add(res);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add((int)vvv[i][3] + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static long[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = new long[4];
                var ys = ReadLongs();
                for (int j = 0; j < 3; j++)
                    vvv[i][j] = ys[j];
                vvv[i][3] = i;
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new long[m, n + 1, 101];
            prev = new int[m, n + 1, 101];
            value = new long[m, n + 1, 101];
            long res = 0;

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, j, u] = -1;

            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, 0, u] = 0;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = a[0] + i;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (dp[i, j, u] >= 0) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            long nv1 = v + dp[i, j, u];
                            long nv2 = vv + dp[i, j, u];
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii] && dp[ii, j + 1, v - a[ii]] < nv1) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = nv1;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii] && dp[ii, j + 1, vv - a[ii]] < nv2) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = nv2;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] >= 0) {
                        find = true;
                        res = Math.Max(res, dp[i, n, u]);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] == res) {
                        idx.Add((int)vvv[i][3] + 1);
                        val.Add(a[i] + u);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add((int)vvv[i][3] + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static bool[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = ReadLongs();
            }

            Array.Sort(vvv, (a, b) => a[2].CompareTo(b[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new bool[m + 1, n + 1, 101];
            prev = new int[m + 1, n + 1, 101];
            value = new long[m + 1, n + 1, 101];
            long res = 0;

            for (int i = 0; a[0] + i <= b[0]; i++) {
                dp[0, 1, i] = true;
            }

            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (j == 0 || dp[i, j, u]) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii]) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = true;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii]) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = true;
                                        //if(ii==3) Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u]) {
                        find = true;
                        res = Math.Max(res, a[i] + u);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] && res == a[i] + u) {
                        idx.Add(i + 1);
                        val.Add(res);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add(i + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


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

namespace Task
{
    class Subj
    {
        public long a, b;
        public int c;
    }

    class Program
    {
        Subj[] subjects;

        bool[, ,] done;
        long[, ,] memo;

        int[, ,] nextSubjId;
        int[, ,] nextComplexityShift;

        int n;
        int k;

        long getMax(int day, int lastSubjId, int complexityShift)
        {
            if (day == n)
                return 0;

            if (!done[day, lastSubjId, complexityShift])
            {
                long ans = -1;
                int bestNextSubjId = -1;
                int bestComplexityShift = -1;

                for (int nextSubjId = 0; nextSubjId < subjects.Length; nextSubjId++)
                {
                    Subj nextSubj = subjects[nextSubjId];
                    if (day != 0 && subjects[lastSubjId].c >= nextSubj.c)
                        continue;

                    if (day == 0)
                    {
                        for (long complexity = nextSubj.a; complexity <= nextSubj.b; complexity++)
                        {
                            int nextShift = (int)(complexity - nextSubj.a);
                            long temp = getMax(day + 1, nextSubjId, nextShift);
                            if (temp == -1)
                                continue;

                            temp += complexity;
                            if (temp > ans)
                            {
                                ans = temp;
                                bestNextSubjId = nextSubjId;
                                bestComplexityShift = nextShift;
                            }
                        }
                    }
                    else
                    {
                        Subj lastSubj = subjects[lastSubjId];
                        for (int mode = 0; mode < 2; mode++)
                        {
                            long nextComplexity = mode == 0 ? (lastSubj.a + complexityShift) * k : ((lastSubj.a + complexityShift) + k);
                            if (nextComplexity < nextSubj.a || nextComplexity > nextSubj.b)
                                continue;

                            int nextShift = (int)(nextComplexity - nextSubj.a);
                            long temp = getMax(day + 1, nextSubjId, nextShift);
                            if (temp == -1)
                                continue;

                            temp += nextComplexity;
                            if (temp > ans)
                            {
                                ans = temp;
                                bestNextSubjId = nextSubjId;
                                bestComplexityShift = nextShift;
                            }
                        }
                    }
                }

                memo[day, lastSubjId, complexityShift] = ans;

                if (ans >= 0)
                {
                    nextSubjId[day, lastSubjId, complexityShift] = bestNextSubjId;
                    nextComplexityShift[day, lastSubjId, complexityShift] = bestComplexityShift;
                }

                done[day, lastSubjId, complexityShift] = true;

            }

            return memo[day, lastSubjId, complexityShift];

        }

        void Solve()
        {
            // Place your code here
            n = io.NextInt();
            int m = io.NextInt();
            k = io.NextInt();

            subjects = new Subj[m];

            for (int i = 0; i < m; i++)
            {
                Subj s = new Subj();
                s.a = io.NextLong();
                s.b = io.NextLong();
                s.c = io.NextInt();
                subjects[i] = s;
            }

            done = new bool[n, m, 101];
            memo = new long[n, m, 101];
            nextSubjId = new int[n, m, 101];
            nextComplexityShift = new int[n, m, 101];

            long ans = getMax(0, 0, 0);

            if (ans == -1)
            {
                io.Print("NO");
                return;
            }

            io.Print("YES");
            int curSubj = 0;
            int curShift = 0;
            for (int day = 0; day < n; day++)
            {
                int nextSubj = nextSubjId[day, curSubj, curShift];
                int nextShift = nextComplexityShift[day, curSubj, curShift];

                io.PrintLine("");
                io.Print("{0} {1}", nextSubj + 1, nextShift + subjects[nextSubj].a);

                curSubj = nextSubj;
                curShift = nextShift;
            }

        }

        MyIo io = new MyIo();

        static void Main(string[] args)
        {
            var p = new Program();

            p.Solve();
            p.io.Close();
        }
    }

    class MyIo
    {
        char[] separators = new char[] { ' ', '\t' };

#if TEST
        TextReader inputStream = new StreamReader("..\\..\\input.txt");
#else
        TextReader inputStream = System.Console.In;
#endif
        TextWriter outputStream = Console.Out;

        string[] tokens;
        int pointer;

        public string NextLine()
        {
            try
            {
                return inputStream.ReadLine();
            }
            catch (IOException)
            {
                return null;
            }
        }

        public string NextString()
        {
            try
            {
                while (tokens == null || pointer >= tokens.Length)
                {
                    tokens = NextLine().Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    pointer = 0;
                }
                return tokens[pointer++];
            }
            catch (IOException)
            {
                return null;
            }
        }

        public int NextInt()
        {
            return int.Parse(NextString());
        }

        public long NextLong()
        {
            return long.Parse(NextString());
        }

        public void Print(string format, params object[] args)
        {
            outputStream.Write(format, args);
        }

        public void PrintLine(string format, params object[] args)
        {
            outputStream.WriteLine(format, args);
        }

        public void Print<T>(T o)
        {
            outputStream.Write(o);
        }

        public void PrintLine<T>(T o)
        {
            outputStream.WriteLine(o);
        }

        public void Close()
        {
            inputStream.Close();
            outputStream.Close();
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round90 {
    class C {
        static int[] ReadInts() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => int.Parse(sss)); }
        static long[] ReadLongs() { return Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), sss => long.Parse(sss)); }

        static int n, m, k;
        static long[] a, b, c;
        static long[, ,] dp;
        static int[, ,] prev;
        static long[, ,] value;

        static void Main() {
            var xs = ReadInts();
            n = xs[0];
            m = xs[1];
            k = xs[2];

            long[][] vvv = new long[m][];
            a = new long[m];
            b = new long[m];
            c = new long[m];

            for (int i = 0; i < m; i++) {
                vvv[i] = new long[4];
                var ys = ReadLongs();
                for (int j = 0; j < 3; j++)
                    vvv[i][j] = ys[j];
                vvv[i][3] = i;
            }

            Array.Sort(vvv, (aa, bb) => aa[2].CompareTo(bb[2]));
            for (int i = 0; i < m; i++) {
                a[i] = vvv[i][0];
                b[i] = vvv[i][1];
                c[i] = vvv[i][2];
            }

            dp = new long[m, n + 1, 101];
            prev = new int[m, n + 1, 101];
            value = new long[m, n + 1, 101];
            long res = 0;

            for (int i = 0; i < m; i++)
                for (int j = 0; j <= n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++)
                        dp[i, j, u] = -1;

            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    dp[i, 1, u] = a[i] + u;

            for (int i = 0; i < m; i++)
                for (int j = 1; j < n; j++)
                    for (int u = 0; a[i] + u <= b[i]; u++) {
                        if (dp[i, j, u] >= 0) {
                            long v = a[i] + u + k,
                                 vv = (a[i] + u) * k;
                            long nv1 = v + dp[i, j, u];
                            long nv2 = vv + dp[i, j, u];
                            for (int ii = i + 1; ii < m; ii++) {
                                if (c[i] < c[ii]) {
                                    if (a[ii] <= v && v <= b[ii] && dp[ii, j + 1, v - a[ii]] < nv1) {
                                        prev[ii, j + 1, v - a[ii]] = i;
                                        value[ii, j + 1, v - a[ii]] = u;
                                        dp[ii, j + 1, v - a[ii]] = nv1;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, v);
                                    }
                                    if (a[ii] <= vv && vv <= b[ii] && dp[ii, j + 1, vv - a[ii]] < nv2) {
                                        prev[ii, j + 1, vv - a[ii]] = i;
                                        value[ii, j + 1, vv - a[ii]] = u;
                                        dp[ii, j + 1, vv - a[ii]] = nv2;
                                        //Console.WriteLine("{0} {1} {2}", ii, j + 1, vv);
                                    }
                                }
                            }
                        }
                    }

            bool find = false;
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] >= 0) {
                        find = true;
                        res = Math.Max(res, dp[i, n, u]);
                    }

            if (!find) {
                Console.WriteLine("NO");
                return;
            }

            Console.WriteLine("YES");
            List<int> idx = new List<int>();
            List<long> val = new List<long>();
            for (int i = 0; i < m; i++)
                for (int u = 0; a[i] + u <= b[i]; u++)
                    if (dp[i, n, u] == res) {
                        idx.Add((int)vvv[i][3] + 1);
                        val.Add(a[i] + u);

                        long v = u;
                        for (int j = n; j > 1; j--) {
                            long vv = v;
                            v = value[i, j, vv];
                            i = prev[i, j, vv];
                            idx.Add((int)vvv[i][3] + 1);
                            val.Add(v+a[i]);
                        }

                        for (int j = idx.Count - 1; j >= 0; j--) {
                            Console.WriteLine("{0} {1}", idx[j], val[j]);
                        }
                        return;
                    }
        }
    }
}


using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = Console.ReadLine();
            data.Remove(0, 1);
            int num = Convert.ToInt32(data);
            if (num % 2 == 0)
                Console.WriteLine("0");
            else Console.WriteLine("1");
        }
    }
}


using System;

class Program
{
  static void Main(string[] args)
  {
    string a = Console.ReadLine();
    a.TrimStart();
    int k = int.Parse(a);
    Console.WriteLine(k % 2);
  }
}


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

namespace Codeforces
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            Random rnd = new Random();
            int q = rnd.Next();
            if (q % 2 == 0)
            {
                Console.WriteLine(q);
            }
            else
                Console.WriteLine(q-1);
        }
    }
}


using System;

class Program
{
  static void Main(string[] args)
  {
    string a = Console.ReadLine();
    a.TrimStart();
    int k = int.Parse(a);
    Console.WriteLine(k % 2)
  }
}


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

public class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine().Split('A')[1]);
		Console.Writeline(n % 2);
	}
}


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

namespace CodeForces1 {
    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 TextWriter writer = Console.Out;
        private static readonly StreamReader reader = new StreamReader(@"..\..\input");
#endif

        static void Main(string[] args) {
            string line = reader.ReadLine();
            int i = int.Parse(line[line.Length - 1].ToString());
            writer.WriteLine(i % 2);

            writer.Flush();
#if !ONLINE_JUDGE
            writer.Close();
#endif
        }

        static int[] ReadArray(int length) {
            int[] result = new int[length];
            for(int i = 0; i < length; i++) {
                result[i] = NextInt();
            }
            return result;
        }
        private static int NextInt() {
            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';
            }
        }
        private static long NextLong() {
            int c;
            long 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KickStartRoundA
{

    class Program
    {

        static void Main(string[] args)
        {

            String s = Console.ReadLine();

            uint t = Convert.ToUInt32(s, 16);

            Console.WriteLine(t % 2);

            }
        }
    }


using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = Console.ReadLine();
            data = data.Remove(0, 1);
            int num = Convert.ToInt32(data);
            if (num % 2 == 0)
                Console.WriteLine("0");
            else Console.WriteLine("1");
        }
    }
}


using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Test
{

	public static void Main()
	{
		string str = Console.ReadLine();
		if (Convert.ToInt32(str[6].ToString()) % 2 == 0)
		{
			Console.WriteLine("0");
		}
		else Console.WriteLine("1");
	}
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            Console.Write(int.Parse(s[s.Length-1].ToString())%2);
        }
    }
}


using System;
using static System.Console;
using static System.Math;
using static System.Array;
using System.IO;

namespace ConsoleApp9
{
    class Program
    {
        static void Conv(string[] a, int[] b)
        {
            for (int i = 0; i < a.Length; i++)
            {
                b[i] = Convert.ToInt32(a[i]);
            }
        }
        static void Print(int[] b)
        {
            for (int i = 0; i < b.Length; i++)
            {
                Write($"{b[i]} ");
            }
            WriteLine();
        }
        static int Main(string[] args)
        {
            // = Convert.ToInt32(cin[0])
            //WriteLine($"{}");
            // string[] cin = ReadLine().Split(' ');
            string a = ReadLine();
            int k = Convert.ToInt32(a[a.Length - 1]);
            WriteLine(k%2);
            return 0;
        }

    }
}


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()
    {
        string s = ReadToken();
        int x = s[6] - '0';
        Write(x % 2);
    }

    #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
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;

namespace ProgrammingContestTemplateByDotNetCore
{
    #region "Input and Output Classes"

    class Scanner : IDisposable
    {
        private readonly Queue<string> _buffer;
        private readonly char[] _sep;
        private readonly TextReader _reader;

        public Scanner(TextReader reader = null, char[] sep = null)
        {
            _buffer = new Queue<string>();
            _sep = sep ?? new[] { ' ' };
            _reader = reader ?? Console.In;
        }

        public Scanner(string path, char[] sep = null)
            : this(new StreamReader(path), sep) { }

        private void CheckBuffer()
        {
            if (_buffer.Any() || _reader.Peek() == -1)
                return;

            string str = string.Empty;
            for (;
                string.IsNullOrWhiteSpace(str);
                str = _reader.ReadLine()) { }

            var values = str.Split(_sep).Where(s => !string.IsNullOrWhiteSpace(s));
            foreach (var item in values)
            {
                _buffer.Enqueue(item);
            }
        }

        public string Next()
        {
            CheckBuffer();
            return _buffer.Dequeue();
        }
        public string[] GetStringArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => Next())
                .ToArray();

        public int NextInt() => int.Parse(Next());
        public int[] GetIntArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextInt())
                .ToArray();

        public long NextLong() => long.Parse(Next());
        public long[] GetLongArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextLong())
                .ToArray();

        public double NextDouble() => double.Parse(Next());
        public double[] GetDoubleArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextDouble())
                .ToArray();

        public decimal NextDecimal() => decimal.Parse(Next());
        public decimal[] GetDecimalArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextDecimal())
                .ToArray();

        public BigInteger NextBigInt() => BigInteger.Parse(Next());
        public BigInteger[] GetBigIntArray(int count)
            => Enumerable.Range(0, count)
                .Select(e => NextBigInt())
                .ToArray();

        public T GetT<T>(Func<Scanner, T> func) => func(this);
        public T[] GetTArray<T>(int count, Func<Scanner, T> func)
            => Enumerable.Range(0, count)
                .Select(e => GetT(func))
                .ToArray();

        public bool IsEnd
        {
            get
            {
                CheckBuffer();
                return !_buffer.Any();
            }
        }

        public void Dispose()
        {
            if (!_reader.Equals(Console.In))
                _reader.Dispose();
        }
    }

    class Writer : IDisposable
    {
        private readonly TextWriter _writer;
        private readonly StringBuilder _cache;
        private readonly bool _isReactive;

        public Writer(TextWriter writer = null, bool isReactive = false)
        {
            _writer = writer ?? Console.Out;
            _isReactive = isReactive;
            if (!_isReactive)
                _cache = new StringBuilder();
        }

        public Writer(string path)
            : this(new StreamWriter(path)) { }

        public Writer(bool isReactive)
            : this(null, isReactive) { }

        public void Write(object value)
        {
            if (_isReactive)
            {
                _writer.Write(value);
                _writer.Flush();
            }
            else
            {
                _cache.Append(value);
            }
        }

        public void WriteFormat(string format, params object[] values)
        {
            var value = string.Format(format, values);
            Write(value);
        }

        public void WriteLine(object value = null)
        {
            Write($"{value}{Environment.NewLine}");
        }

        public void WriteLine(string format, params object[] values)
        {
            WriteFormat($"{format}{Environment.NewLine}", values);
        }

        public void Dispose()
        {
            if (!_isReactive)
            {
                _writer.Write(_cache);
                _writer.Flush();
            }
            if (!_writer.Equals(Console.Out))
            {
                _writer.Dispose();
            }
        }
    }

    public static class Extensions
    {
        public static void Set<TKey, TValue>(
            this IDictionary<TKey, TValue> source,
            TKey key, TValue value)
        {
            if (source.ContainsKey(key))
                source[key] = value;
            else
                source.Add(key, value);
        }

        public static void Update<TKey, TValue>(
            this IDictionary<TKey, TValue> source,
            TKey key, Func<TValue, TValue> update)
        {
            if (!source.ContainsKey(key))
                source.Add(key, default(TValue));
            source[key] = update(source[key]);
        }
    }

#endregion

    class MainClass : IDisposable
    {
#region "Template"

        /// <summary> 入力を受け取る </summary>
        private readonly Scanner sc;
        /// <summary> 出力を行う </summary>
        private readonly Writer wr;
        private const string _inputFile = "input.txt";
        private const string _outFile = "output.txt";

        static void Main(string[] args)
        {
            using (new MainClass()) { }
        }

        public MainClass()
        {
            wr = new Writer(_isReactive);
            // TODO: ファイルに出力したい場合はこっち → wr = new Writer(_outFile);

#if DEBUG
            // 手元では、ファイルから入力を受け取る
            sc = new Scanner(_inputFile);
#else

            // 提出時、標準入力から受け取る
            sc = new Scanner();
#endif

            Solve();
        }

        public void Dispose()
        {
            sc?.Dispose();
            wr?.Dispose();
        }

        #endregion

        void Solve()
        {
            var s = sc.Next();
            wr.WriteLine((s.Last() - '0') % 2);
        }

        /// <summary>
        /// TODO: リアクティブ問題か否かを指定してね♪
        /// </summary>
        private const bool _isReactive = false;
    }
}


using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
           string a = Console.ReadLine();
            if (Convert.ToInt32(a[a.Length - 1]) % 2 == 0)
                Console.WriteLine(0);
            else Console.WriteLine(1);
        }
    }
}


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

namespace AprilFools
{
    class Program
    {
        static void Main(string[] args)
        {
            string t = Console.ReadLine();
            int q = Convert.ToInt32(t[6]);
            Console.WriteLine(q%2);
        }
    }
}


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

namespace D
{
    class Program
    {
        static void Main(string[] args)
        {
            TextReader sr = Console.In;
            //StreamReader sr = new StreamReader("1.txt");
            string s = sr.ReadLine();
            Console.WriteLine((Convert.ToInt32(s[6])%2).ToString());
        }
    }
}


using System;
using System.Linq;

namespace a
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = Console.ReadLine();

            var a = Convert.ToInt32(x[x.Length -1]);

            Console.WriteLine(a%2);
        }
    }
}


using System;

class Program
{
  static void Main(string[] args)
  {
    string a = Console.ReadLine();
    a = a.Remove(0, 1);
    int k = int.Parse(a);
    Console.WriteLine(k % 2);
  }
}


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

namespace prD {
    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) {
            var q = int.Parse(reader.ReadLine(), NumberStyles.AllowHexSpecifier);
            writer.WriteLine(q & 1);
            writer.Flush();
#if !ONLINE_JUDGE
            writer.Close();
#endif
        }
        private static int NextInt() {
            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';
            }
        }
        private static long NextLong() {
            int c;
            long 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;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine().Split('A')[1]);
		Console.WriteLine(n % 2);
	}
}


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

namespace ConsoleApp1
{
    public class Program
    {
        static void Main(string[] args)
        {
            var readLine = Console.ReadLine();
            var number = int.Parse(readLine.Last().ToString());
            Console.WriteLine(number%2);
        }
    }
}


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

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            string s1 = ReadLine();
            string s2 = ReadLine();
            int n = s1.Length;

            var a = new int[n + 1, 4];
            for (int i = 0; i < n + 1; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    a[i, j] = -1;
                }
            }
            a[0, 3] = 0;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    bool l1 = j < 2;
                    bool l2 = (j % 2 == 0);
                    bool r1 = s1[i] == '0';
                    bool r2 = s2[i] == '0';

                    int mask = 0;
                    if (!r1)
                    {
                        mask += 2;
                    }
                    if (!r2)
                    {
                        mask += 1;
                    }

                    a[i + 1, mask] = Math.Max(a[i + 1, mask], a[i, j]);
                    if (l1 && l2 && r1)
                    {
                        a[i + 1, 2 | mask] = Math.Max(a[i + 1, 2 | mask], a[i, j] + 1);
                    }
                    if (l1 && l2 && r2)
                    {
                        a[i + 1, 1 | mask] = Math.Max(a[i + 1, 1 | mask], a[i, j] + 1);
                    }
                    if (l1 && r1 && r2)
                    {
                        a[i + 1, 3 | mask] = Math.Max(a[i + 1, 3 | mask], a[i, j] + 1);
                    }
                    if (l2 && r1 && r2)
                    {
                        a[i + 1, 3 | mask] = Math.Max(a[i + 1, 3 | mask], a[i, j] + 1);
                    }
                }
            }

            int ans = 0;
            for (int i = 0; i < 4; i++)
            {
                ans = Math.Max(ans, a[n, i]);
            }
            Writer.WriteLine(ans);
        }

        public static void Solve()
        {
            SolveCase();

            /*var sw = Stopwatch.StartNew();*/

            /*int T = ReadInt();
            for (int i = 0; i < T; i++)
            {
                Writer.Write("Case #{0}: ", i + 1);
                SolveCase();
            }*/

            /*sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);*/
        }

        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            //Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            // Solve();
            Thread thread = new Thread(Solve, 64 * 1024 * 1024);
            thread.CurrentCulture = CultureInfo.InvariantCulture;
            thread.Start();
            thread.Join();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Random rnd = new Random();
            for (int i = result.Length - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = result[k];
                result[k] = result[i];
                result[i] = tmp;
            }
            return result;
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        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 string ReadLine()
        {
            return Reader.ReadLine();
        }

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

        #endregion
    }
}


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

namespace Task
{
  class Program
  {
    #region Read / Write
    protected static TextReader reader;
    protected static TextWriter writer;
    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=

    public static bool NextPermutation(List<int> perm)
    {
      for (int i = perm.Count - 2; i >= 0; i--)
      {
        if (perm[i] < perm[i + 1])
        {
          int min = i + 1;
          for (int j = min; j < perm.Count; j++)
            if (perm[j] > perm[i] && perm[j] < perm[min])
              min = j;

          int tmp = perm[i];
          perm[i] = perm[min];
          perm[min] = tmp;
          perm.Reverse(i + 1, perm.Count - i - 1);
          return true;
        }
      }

      return false;
    }

    static void Main(string[] args)
    {
      long i = 0;
      long ans = 0;
      reader = new StreamReader(Console.OpenStandardInput());
      writer = new StreamWriter(Console.OpenStandardOutput());

      var s = ReadLines(2);
      var mas = s.Select(it => it.Select(ch => ch == 'X' ? 1 : 0).ToArray()).ToArray();

      int n = mas[0].Length;
      if (n < 2)
      {
        Write(0);
      }
      else
      {
        for (i = 0; i < n - 1; i++)
        {
          int ones = mas[0][i] + mas[0][i + 1] + mas[1][i] + mas[1][i + 1];
          if (ones == 1)
          {
            ans++;
            mas[0][i] = mas[0][i + 1] = mas[1][i] = mas[1][i + 1] = 1;
          }
          else if (ones == 0)
          {
            ans++;
            mas[0][i] = mas[0][i + 1] = mas[1][i] = 1;
          }
        }
        Write(ans);
      }

      reader.Close();
      writer.Close();
    }
  }
}


using System;

namespace ShadowingOverride
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = new string[2];
            s[0] = Console.ReadLine();
            s[1] = Console.ReadLine();
            int n = s[0].Length;
            int empty = 0;
            int cur = 0;
            int ans = 0;

            for(int i = 0; i < n; i++)
            {
                cur = (s[0][i] == '0'?1:0) + (s[1][i] == '0'?1:0);
              //  Console.WriteLine(cur);
                empty += cur;
                if( empty >= 3)
                {
                    empty -= 3;
                    ans++;
                }
                else
                {
                    empty = cur;
                }
            }
            Console.WriteLine(ans);
        }
    }
}


using System;
using System.Collections.Generic;

namespace Problems
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] s1 = Console.ReadLine().ToCharArray();
            char[] s2 = Console.ReadLine().ToCharArray();

            int count = 0;

            for (int i = 0; i < s1.Length - 1; i++)
            {
                if(s1[i] == '0' && s2[i] == '0')
                {
                    if(s1[i + 1] == '0')
                    {
                        s1[i] = 'X';
                        s2[i] = 'X';
                        s1[i + 1] = 'X';
                        count++;
                    }
                    else if(s2[i + 1] == '0')
                    {
                        s1[i] = 'X';
                        s2[i] = 'X';
                        s2[i + 1] = 'X';
                        count++;
                    }
                }
                else if(s1[i] == '0')
                {
                    if(s1[i + 1] == '0' && s2[i + 1] == '0')
                    {
                        s1[i] = 'X';
                        s1[i + 1] = 'X';
                        s2[i + 1] = 'X';
                        count++;
                    }
                }
                else if(s2[i] == '0')
                {
                    if(s1[i + 1] == '0' && s2[i + 1] == '0')
                    {
                        s2[i] = 'X';
                        s1[i + 1] = 'X';
                        s2[i + 1] = 'X';
                        count++;
                    }
                }
            }

            Console.WriteLine(count);
        }
    }
}


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

class Program
{
    const bool TEST_MODE = false;

    static void Main()
    {
        if (TEST_MODE)
        {
            string[] readText = File.ReadAllLines("..\\..\\TextFile1.txt");

            var testCases = new Dictionary<List<string>, string>();
            var lastKeyword = "";
            var current = new List<string>();
            foreach (string s in readText)
            {
                if(s == "Input")
                {
                    current = new List<string>();
                    lastKeyword = s;
                    continue;
                }
                if (s == "Copy")
                    continue;
                if (s == "Output")
                {
                    lastKeyword = s;
                    continue;
                }

                if (lastKeyword == "Input")
                    current.Add(s);
                if (lastKeyword == "Output")
                    testCases.Add(current,s);

            }

            foreach(var testCase in testCases)
            {
                var result = ProblemWrapper.Solve(testCase.Key.ToArray(), testCase.Value);
                if (result == testCase.Value)
                    Console.ForegroundColor = ConsoleColor.Green;
                else
                    Console.ForegroundColor = ConsoleColor.Red;

                Console.Write("input: ");
                foreach (var input in testCase.Key)
                    Console.Write(input + " // ");
                Console.Write("\noutput: ");
                Console.WriteLine(testCase.Value);

                Console.Write("Actual: ");
                Console.WriteLine(result);

                Console.ResetColor();
            }
        }
        else
            ProblemWrapper.Solve();

        Console.ReadLine();
    }
}

class ProblemWrapper
{
    public static void Solve()
    {
        string[] input = new string[2];
        input[0] = Console.ReadLine();
        input[1] = Console.ReadLine();

        var a = ReadString(input[0]);
        var b = ReadString(input[1]);

        Console.WriteLine (Algo(a, b));
    }

    public static string Solve(string[] input, string output)
    {
        var a = ReadString(input[0]);
        var b = ReadString(input[1]);

        return (Algo(a, b));
    }

    public static string Algo(string a, string b)
    {
        int answer = 0;

        //**
        {
            var row1 = a.ToCharArray();
            var row2 = b.ToCharArray();

            var n = row1.Length;
            if (n < 2)
                return "0";

            for (var i = 0; i < n; i++)
            {
                if (row1[i] == '0')
                {
                    if (i != 0 && row2[i - 1] == '0' && row2[i] == '0')
                    {
                        row1[i] = 'X'; row2[i] = 'X'; row2[i - 1] = 'X';
                        answer++;
                    }
                    else if (i != 0 && row2[i] == '0' && row1[i - 1] == '0')
                    {
                        row1[i] = 'X'; row2[i] = 'X'; row1[i - 1] = 'X';
                        answer++;
                    }
                    else if (i != n - 1 && row2[i] == '0' && row2[i + 1] == '0')
                    {
                        row1[i] = 'X'; row2[i] = 'X'; row2[i + 1] = 'X';
                        answer++;
                    }
                    else if (i != n - 1 && row2[i] == '0' && row1[i + 1] == '0')
                    {
                        row1[i] = 'X'; row2[i] = 'X'; row1[i + 1] = 'X';
                        answer++;
                    }
                }

            }
        }
        //**

        return Convert.ToString(answer);
    }

    static String   ReadString(string input)    { return input; }
    static String[] ReadStringArr(string input) { return input.Split(' '); }
    static int      ReadInt(string input)       { return int.Parse(input); }
    static long     ReadLong(string input)      { return long.Parse(input); }
    static double   ReadDouble(string input)    { return double.Parse(input); }
    static int[]    ReadIntArr(string input)    { return Array.ConvertAll(input.Split(' '), e =>    int.Parse(e)); }
    static long[]   ReadLongArr(string input)   { return Array.ConvertAll(input.Split(' '), e =>   long.Parse(e)); }
    static double[] ReadDoubleArr(string input) { return Array.ConvertAll(input.Split(' '), e => double.Parse(e)); }
}


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

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){

		if(N == 1){
			Console.WriteLine(0);
			return;
		}
		int H = 2;
		int W = N;

		int M = 1<<4;
		int[] c = new int[]{
			0xF - 1,
			0xF - 2,
			0xF - 4,
			0xF - 8,
			0
		};
		int[] dp = new int[M];
		for(int i=0;i<M;i++) dp[i] = -1;
		int s = 0;
		for(int i=0;i<2;i++){
			for(int j=0;j<H;j++){
				s |= (S[j][i] == 'X' ? 1 : 0) << (i * 2 + j);
			}
		}
//Console.WriteLine(s);
		dp[s] = 0;

		for(int t=0;t<N-1;t++){
//Console.WriteLine("dp:{0}",String.Join(" ",dp));
			int[] ndp = new int[M];
			for(int i=0;i<M;i++) ndp[i] = -1;
			for(int i=0;i<M;i++){
				if(dp[i] == -1) continue;
				foreach(var b in c){
					if((i & b) != 0) continue;
					var st = (i | b) >> 2;
					for(int j=0;j<2;j++){
						st |= ((t + 2 < N && S[j][t + 2] == 'X') ? 1 : 0) << (2 + j);
					}
//Console.WriteLine("i:{0}, b:{1}, st:{2}",i, b, st);
					ndp[st] = Math.Max(ndp[st], dp[i] + (b == 0 ? 0 : 1));
				}
			}
			dp = ndp;
//Console.WriteLine("ndp:{0}",String.Join(" ",dp));
		}

		Console.WriteLine(dp.Max());

	}
	int N;
	String[] S;
	public Sol(){
		S = new String[2];
		for(int i=0;i<2;i++) S[i] = rs();
		N = S[0].Length;
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}