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

namespace RocketB
{
    class Program
    {

        private class Bid
        {
            public int Index;
            public bool Left;
            public int Value;
            public double[] WinRate;
        }

        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int n = 0;
            int.TryParse(str, out n);

            var R = new int[n];
            var L = new int[n];
            int min = 100000;
            int max = 0;
            for(int i = 0; i < n; i++)
            {
                str = Console.ReadLine();
                var strs = str.Split(' ');
                int l = 0;
                int r = 0;
                int.TryParse(strs[0], out l);
                int.TryParse(strs[1], out r);
                L[i] = l;
                R[i] = r;
                if(l < min) min = l;
                if(r > max) max = r;
            }

            double expected = 0d;

            //var debug = new double[max + 1];
            //double weightSum =0;
            for (int price = min; price <= max; price++)
            {

                double weight = 0;
                double exactlyOne = 0;
                double less = 1d;
                double notMore = 1d;
                for(int i =0; i < n; i++)
                {
                    var ri = R[i];
                    var li = L[i];
                    var di = ri - li + 1;
                    double imoreprob;
                    double iNotMoreProb;
                    double here = 0d;
                    if(li > price)
                    {
                        less = 0;
                        notMore = 0;
                        imoreprob = 1;
                        iNotMoreProb = 0;
                    }
                    else
                        if(ri < price)
                        {
                            imoreprob = 0;
                            iNotMoreProb = 1;
                            //less *= 1;
                            //notMore *= 1;
                            continue;
                        }
                        else
                        {
                            imoreprob = (ri - price) / (double)di;
                            notMore *=  (price - li + 1) / (double)di;
                            less *= (price - li)  / (double)di;
                            here = 1 / (double)di;
                        }

                    double allNotMore = 1d;
                    double allLess = 1d;

                    for(int j =0; j <n; j++)
                    {
                        if (j == i) continue;
                        var rj = R[j];
                        var lj = L[j];
                        var dj = rj - lj + 1;

                        if(lj > price)
                        {
                            allNotMore = allLess = 0;
                        }
                        else
                        if(rj < price)
                        {
                            //allNotMore *=1 ; allLess *= 1;
                        }
                        else
                        {
                            allNotMore *= (price - lj + 1) /(double)dj;
                            allLess *= (price - lj) / (double)dj;
                        }
                    }

                    weight += imoreprob * (allNotMore - allLess);
                    exactlyOne += here * allLess;
                    //weight += here *
                }
                weight += (notMore - less - exactlyOne);//2 or more here
                //debug[price] = weight;
               // weightSum += weight;
                expected += price * weight;
            }

            Console.WriteLine((expected).ToString("F10", CultureInfo.InvariantCulture)); ;

        }
    }
}


using System;
using System.Collections.Generic;
using System.Collections;
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()
        {
            return double.Parse(ReadNextToken());
        }

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

        public void Write(double val, int precision)
        {
            Int64 valI = (Int64)val;
            writer.Write(valI.ToString());
            double diff = val - valI;
            if (precision > 0)
                writer.Write(".");
            int i;
            for (i = 0; i < precision; i++)
            {
                diff *= 10;
                writer.Write(((int)diff).ToString());
                diff -= (int)diff;
            }
        }

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

        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;
        int[] l;
        int[] r;
        double[] probThisResult;
        double[] EVThisResult;

        public void Solve()
        {
            n = ioHelper.ReadNextInt();
            l = new int[n];
            r = new int[n];

            probThisResult = new double[10002];
            EVThisResult = new double[10002];

            int i;
            for (i = 0; i < n; i++)
            {
                l[i] = ioHelper.ReadNextInt();
                r[i] = ioHelper.ReadNextInt();
            }

            int winningBid = 1;
            for (; winningBid <= 10000; winningBid++)
            {
                double probabilityNone = 1.0;
                double probabilityOne = 0;
                double probabilityAtLeastTwo = 0;

                for (int loser = 0; loser < n; loser++)
                {
                    double hisProbWinning = 1.0;
                    if (winningBid < l[loser] || winningBid > r[loser])
                        hisProbWinning = 0;

                    double hisProbLesserThan = (winningBid - l[loser]);
                    if (winningBid < l[loser])
                        hisProbLesserThan = 0.0;
                    if (winningBid > r[loser])
                        hisProbLesserThan = r[loser] - l[loser] + 1.0;

                    probabilityAtLeastTwo = probabilityOne * hisProbWinning + probabilityAtLeastTwo * (hisProbLesserThan + hisProbWinning);
                    probabilityOne = probabilityNone * hisProbWinning + probabilityOne * (hisProbLesserThan);
                    probabilityNone = probabilityNone * hisProbLesserThan;

                    probabilityAtLeastTwo /= (r[loser] - l[loser] + 1.0);
                    probabilityOne /= (r[loser] - l[loser] + 1.0);
                    probabilityNone /= (r[loser] - l[loser] + 1.0);
                }

                probThisResult[winningBid] += probabilityAtLeastTwo;
            }

            int secondBid = 1;
            for (; secondBid < 10001; secondBid++)
            {
                double probAtLeastOneNoWinner = 0;
                double probAtLeastOneWinner = 0;
                double probNoneNoWinner = 1.0;
                double probNoneOneWinner = 0.0;

                for (int loser = 0; loser < n; loser++)
                {
                    double hisProbSecond = 1.0;
                    if (secondBid < l[loser] || secondBid > r[loser])
                        hisProbSecond = 0;

                    double hisProbLesserThan = (secondBid - l[loser]);
                    if (secondBid < l[loser])
                        hisProbLesserThan = 0;
                    if (secondBid > r[loser])
                        hisProbLesserThan = 1.0 * (r[loser]-l[loser]+1.0);

                    double hisProbWinner = (r[loser] - secondBid);
                    if (secondBid + 1 > r[loser])
                        hisProbWinner = 0;
                    if (secondBid + 1 < l[loser])
                        hisProbWinner = 1.0 * (r[loser]-l[loser]+1.0);

                    double pnnw = probNoneNoWinner * hisProbLesserThan;
                    double pnw = probNoneNoWinner * hisProbWinner + probNoneOneWinner * (hisProbLesserThan);
                    double panw = probAtLeastOneNoWinner * (hisProbLesserThan + hisProbSecond) + probNoneNoWinner * (hisProbSecond);
                    double paw = probAtLeastOneWinner * (hisProbLesserThan+hisProbSecond) + probAtLeastOneNoWinner * hisProbWinner + probNoneOneWinner * hisProbSecond;

                    probNoneNoWinner = pnnw;
                    probNoneOneWinner = pnw;
                    probAtLeastOneNoWinner = panw;
                    probAtLeastOneWinner = paw;

                    probNoneNoWinner /= (r[loser] - l[loser] + 1.0);
                    probNoneOneWinner /= (r[loser] - l[loser] + 1.0);
                    probAtLeastOneNoWinner /= (r[loser] - l[loser] + 1.0);
                    probAtLeastOneWinner /= (r[loser] - l[loser] + 1.0);
                }

                probThisResult[secondBid] += probAtLeastOneWinner;
            }

            double result = 0;
            for (i = 0; i < 10001; i++)
                result += probThisResult[i] * i;

            ioHelper.Write(result, 12);
            ioHelper.WriteLine("");
            ioHelper.Flush();
        }

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


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

namespace RocketB
{
    class Program
    {

        private class Bid
        {
            public int Index;
            public bool Left;
            public int Value;
            public double[] WinRate;
        }

        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int n = 0;
            int.TryParse(str, out n);

            var R = new int[n];
            var L = new int[n];
            int min = 100000;
            int max = 0;
            for(int i = 0; i < n; i++)
            {
                str = Console.ReadLine();
                var strs = str.Split(' ');
                int l = 0;
                int r = 0;
                int.TryParse(strs[0], out l);
                int.TryParse(strs[1], out r);
                L[i] = l;
                R[i] = r;
                if(l < min) min = l;
                if(r > max) max = r;
            }

            double expected = 0d;

            //var debug = new double[max + 1];
            //double weightSum =0;
            var more = new double[n];
            var notMore = new double[n];
            var less = new double[n];
            var here = new double[n];
            for (int price = min; price <= max; price++)
            {

                double weight = 0;
                double exactlyOne = 0;
                double allLess = 1d;
                double allNotMore = 1d;

                bool LessNotZeroed = true;
                bool NotMoreZeroed = true;
                for (int i = 0; i < n; i++)
                {
                    var ri = R[i];
                    var li = L[i];
                    var di = ri - li + 1;
                    double currentLess, currentNotMore;
                    double currentHere, currentMore;
                    if (li > price)
                    {
                        currentLess = 0;
                        currentNotMore = 0;
                        currentMore = 1;
                        currentHere = 0;
                    }
                    else if (ri < price)
                    {
                        currentMore = 0;
                        currentLess = 1;
                        currentNotMore = 1;
                        currentHere = 0;
                    }
                    else
                    {
                        currentMore = (ri - price) / (double)di;
                        currentNotMore = (price - li + 1) / (double)di;
                        currentLess = (price - li) / (double)di;
                        currentHere = 1 / (double)di;
                    }

                    more[i] = currentMore;
                    notMore[i] = currentNotMore;
                    less[i] = currentLess;
                    here[i] = currentHere;

                    if (currentLess == 0 && LessNotZeroed)
                    {
                        LessNotZeroed = false;
                    }
                    else
                        allLess *= currentLess;

                    if (currentNotMore == 0 && NotMoreZeroed)
                    {
                        NotMoreZeroed = false;
                    }
                    else
                    allNotMore *= currentNotMore;

                }
                for(int i =0; i < n; i++)
                {
                    double allOtherNotMore = NotMoreZeroed ? allNotMore / notMore[i] : (notMore[i] == 0 ? allNotMore : 0);
                    double allOtherLess = LessNotZeroed ? allLess / less[i] : (less[i] == 0 ? allLess : 0);
                    weight += more[i] * (allOtherNotMore - allOtherLess); //1 more all other less
                    exactlyOne += here[i] * allOtherLess;
                }
                if (NotMoreZeroed)
                    weight += (allNotMore - (LessNotZeroed ? allLess : 0 ) - exactlyOne);//2 or more here all other less
                //debug[price] = weight;
               // weightSum += weight;
                expected += price * weight;
            }
            Console.WriteLine((expected).ToString("F10", CultureInfo.InvariantCulture));
        }
    }
}


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

namespace Codeforces
{
    class C
    {
        private static ThreadStart s_threadStart = new C().Go;
        private static bool s_time = false;
        private static double s_eps = 1e-16;

        int n;
        List<int[]> ranges;
        int[] t;

        double P(int x)
        {
            double p = 0.0;

            for (int i = 0; i < n; i++)
            {
                t[i] = i;
            }

            do
            {
                double xx = 1.0;

                //all equal
                if (n > 1)
                {
                    xx = 1.0;
                    for (int i = 0; i < n; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    p += xx;
                }

                //all but one equal
                if (n > 2)
                {
                    xx = 1.0;
                    for (int i = 0; i < n - 1; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    xx *= (1.0 - singleP(t[n - 1], x));
                    p += xx;
                }

                //all but 2 equal
                if (n > 3)
                {
                    xx = 1.0;
                    for (int i = 0; i < n - 2; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    xx *= (biggerP(t[n - 1], x) + smallerP(t[n - 1], x) * 0.5) * smallerP(t[n - 2], x);
                    p += xx;
                }

                //all but 3 equal = 2 are equal
                if (n == 5)
                {
                    xx = 0.5;
                    xx *= singleP(t[0], x);
                    xx *= singleP(t[1], x);
                    xx *= (biggerP(t[2], x) * 0.5 + smallerP(t[2], x) / 6) * smallerP(t[3], x) * smallerP(t[4], x);

                    p += xx;
                }

                //only one equal

                xx = singleP(t[0], x);
                xx *= biggerP(t[1], x);
                for (int i = 2; i < n; i++)
                {
                    xx *= smallerP(t[i], x) / (i - 1);
                }

                p += xx;

            } while (NextPermutation(t));

            return p;
        }

        double singleP(int i, int x)
        {
            if (x < ranges[i][0] || x > ranges[i][1]) return 0.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            return 1.0 / span;
        }

        double biggerP(int i, int x)
        {
            if (x > ranges[i][1]) return 0.0;
            if (x < ranges[i][0]) return 1.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            double num = ranges[i][1] - x;
            return num / span;
        }

        double smallerP(int i, int x)
        {
            if (x > ranges[i][1]) return 1.0;
            if (x < ranges[i][0]) return 0.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            double num = x - ranges[i][0];
            return num / span;
        }

        private void Go()
        {
            n = GetInt();
            ranges = new List<int[]>(n);
            t = new int[n];
            for (int i = 0; i < n; i++)
            {
                ranges.Add(new int[] { GetInt(), GetInt() });
            }

            double ans = 0.0;
            for (int i = 1; i <= 10000; i++)
            {
                ans += P(i) * i;
            }

            Wl(ans);
        }

        public static bool NextPermutation<T>(T[] arr) where T : IComparable<T>
        {
            return NextPermutation(arr, (a, b) => a.CompareTo(b));
        }

        public static bool NextPermutation<T>(T[] arr, Comparison<T> comp)
        {
            return NextPermutation(arr, 0, arr.Length, comp);
        }

        public static bool NextPermutation<T>(T[] arr, int start, int count, Comparison<T> comp)
        {
            if (count < 2) return false;

            for (int i = start + count - 2; i >= start; --i)
            {
                if (comp(arr[i], arr[i + 1]) < 0)
                {
                    for (int j = start + count - 1; ; --j)
                    {
                        if (comp(arr[i], arr[j]) < 0)
                        {
                            T tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
                            Array.Reverse(arr, i + 1, count - (i + 1 - start));
                            return true;
                        }
                    }
                }
            }

            Array.Reverse(arr, start, count);
            return false;
        }

        #region Template

        public static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            Thread main = new Thread(new ThreadStart(s_threadStart), 200 * 1024 * 1024);
            timer.Start();
            main.Start();
            main.Join();
            timer.Stop();
            if (s_time)
                Wl(timer.ElapsedMilliseconds);
        }

        private static IEnumerator<string> ioEnum;
        private static string GetString()
        {
            while (ioEnum == null || !ioEnum.MoveNext())
            {
                ioEnum = Console.ReadLine().Split().AsEnumerable().GetEnumerator();
            }

            return ioEnum.Current;
        }

        private static int GetInt()
        {
            return int.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static long GetLong()
        {
            return long.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static double GetDouble()
        {
            return double.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static List<int> GetIntArr(int n)
        {
            List<int> ret = new List<int>(n);
            for (int i = 0; i < n; i++)
            {
                ret.Add(GetInt());
            }
            return ret;
        }

        private static void Wl<T>(T o)
        {
            if (o is double)
            {
                Wld((o as double?).Value, "");
            }
            else if (o is float)
            {
                Wld((o as float?).Value, "");
            }
            else
                Console.WriteLine(o.ToString());
        }

        private static void Wl<T>(IEnumerable<T> enumerable)
        {
            Wl(string.Join(" ", enumerable.Select(e => e.ToString()).ToArray()));
        }

        private static void Wld(double d, string format)
        {
            Wl(d.ToString(format, CultureInfo.InvariantCulture));
        }

        #endregion
    }
}


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

namespace codeforces
{

    public class Program
    {
        private static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            var L = new int[n];
            var R = new int[n];
            for (int i = 0; i < n; ++i)
            {
                var values = Console.ReadLine().Split(' ');
                L[i] = Convert.ToInt32(values[0]);
                R[i] = Convert.ToInt32(values[1]);
            }

            double result = 0.0;
            for (int i = 0; i < n; ++i)
            {
                for (int x = 1; x <= R[i]; ++x)
                {
                    for (int mask = 1; mask < (1 << (n - 1)); ++mask)
                    {
                        double p = 1.0;
                        int one = 0;
                        for (int j = 0; j < n - 1; ++j)
                        {
                            int index = j >= i ? j + 1 : j;
                            if ((mask & (1 << j)) == 0)
                            {
                                if (L[index] > x)
                                {
                                    p = 0.0;
                                }
                                else
                                {
                                    p *= 1.0 * (Math.Min(R[index] + 1, x) - L[index]) / (R[index] - L[index] + 1);
                                }
                            }
                            else
                            {
                                one++;
                                if (x >= L[index] && x <= R[index])
                                {
                                    p *= 1.0 / (R[index] - L[index] + 1);
                                }
                                else
                                {
                                    p = 0.0;
                                }
                            }
                        }
                        if (x < L[i])
                        {
                            result += x * p;
                        }
                        else
                        {
                            result += x * p * (1.0 * (R[i] - x) / (R[i] - L[i] + 1) + 1.0 / (R[i] - L[i] + 1) * 1.0 / (one + 1));
                        }

                    }
                }
            }
            Console.WriteLine(result);
        }
    }
}


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

namespace RocketB
{
    class Program
    {

        private class Bid
        {
            public int Index;
            public bool Left;
            public int Value;
            public double[] WinRate;
        }

        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int n = 0;
            int.TryParse(str, out n);

            var R = new int[n];
            var L = new int[n];
            int min = 100000;
            int max = 0;
            for(int i = 0; i < n; i++)
            {
                str = Console.ReadLine();
                var strs = str.Split(' ');
                int l = 0;
                int r = 0;
                int.TryParse(strs[0], out l);
                int.TryParse(strs[1], out r);
                L[i] = l;
                R[i] = r;
                if(l < min) min = l;
                if(r > max) max = r;
            }

            double expected = 0d;

            //var debug = new double[max + 1];
            //double weightSum =0;
            for (int price = min; price <= max; price++)
            {

                double weight = 0;
                double exactlyOne = 0;
                double less = 1d;
                double notMore = 1d;
                for(int i =0; i < n; i++)
                {
                    var ri = R[i];
                    var li = L[i];
                    var di = ri - li + 1;
                    double imoreprob;
                    double iNotMoreProb;
                    double here = 0d;
                    if(li > price)
                    {
                        less = 0;
                        notMore = 0;
                        imoreprob = 1;
                        iNotMoreProb = 0;
                    }
                    else
                        if(ri < price)
                        {
                            imoreprob = 0;
                            iNotMoreProb = 1;
                            //less *= 1;
                            //notMore *= 1;
                            continue;
                        }
                        else
                        {
                            imoreprob = (ri - price) / (double)di;
                            notMore *=  (price - li + 1) / (double)di;
                            less *= (price - li)  / (double)di;
                            here = 1 / (double)di;
                        }

                    double allNotMore = 1d;
                    double allLess = 1d;

                    for(int j =0; j <n; j++)
                    {
                        if (j == i) continue;
                        var rj = R[j];
                        var lj = L[j];
                        var dj = rj - lj + 1;

                        if(lj > price)
                        {
                            allNotMore = allLess = 0;
                        }
                        else
                        if(rj < price)
                        {
                            //allNotMore *=1 ; allLess *= 1;
                        }
                        else
                        {
                            allNotMore *= (price - lj + 1) /(double)dj;
                            allLess *= (price - lj) / (double)dj;
                        }
                    }

                    weight += imoreprob * (allNotMore - allLess);
                    exactlyOne += here * allLess;
                    //weight += here *
                }
                weight += (notMore - less - exactlyOne);//2 or more here
                //debug[price] = weight;
               // weightSum += weight;
                expected += price * weight;
            }

            Console.WriteLine(expected);

        }
    }
}


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

namespace Codeforces
{
    class C
    {
        private static ThreadStart s_threadStart = new C().Go;
        private static bool s_time = false;
        private static double s_eps = 1e-16;

        int n;
        List<int[]> ranges;
        int[] t;

        double P(int x)
        {
            double p = 0.0;

            for (int i = 0; i < n; i++)
            {
                t[i] = i;
            }

            do
            {
                double xx = 1.0;

                //all equal
                if (n > 1)
                {
                    xx = 1.0;
                    for (int i = 0; i < n; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    p += xx;
                }

                //all but one equal
                if (n > 2)
                {
                    xx = 1.0;
                    for (int i = 0; i < n - 1; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    xx *= (1.0 - singleP(t[n - 1], x));
                    p += xx;
                }

                //all but 2 equal
                if (n > 3)
                {
                    xx = 1.0;
                    for (int i = 0; i < n - 2; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    xx *= (biggerP(t[n - 1], x) + smallerP(t[n - 1], x) * 0.5) * smallerP(t[n - 2], x);
                    p += xx;
                }

                //all but 3 equal = 2 are equal
                if (n == 5)
                {
                    xx = 0.0;
                    xx *= singleP(t[0], x);
                    xx *= singleP(t[1], x);
                    xx *= (biggerP(t[2], x) * 0.5 + smallerP(t[2], x) / 6) * smallerP(t[3], x) * smallerP(t[4], x);
                }

                //only one equal

                xx = singleP(t[0], x);
                xx *= biggerP(t[1], x);
                for (int i = 2; i < n; i++)
                {
                    xx *= smallerP(t[i], x) / (i - 1);
                }

                p += xx;

            } while (NextPermutation(t));

            return p;
        }

        double singleP(int i, int x)
        {
            if (x < ranges[i][0] || x > ranges[i][1]) return 0.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            return 1.0 / span;
        }

        double biggerP(int i, int x)
        {
            if (x > ranges[i][1]) return 0.0;
            if (x < ranges[i][0]) return 1.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            double num = ranges[i][1] - x;
            return num / span;
        }

        double smallerP(int i, int x)
        {
            if (x > ranges[i][1]) return 1.0;
            if (x < ranges[i][0]) return 0.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            double num = x - ranges[i][0];
            return num / span;
        }

        private void Go()
        {
            n = GetInt();
            ranges = new List<int[]>(n);
            t = new int[n];
            for (int i = 0; i < n; i++)
            {
                ranges.Add(new int[] { GetInt(), GetInt() });
            }

            double ans = 0.0;
            for (int i = 1; i <= 10000; i++)
            {
                ans += P(i) * i;
            }

            Wl(ans);
        }

        public static bool NextPermutation<T>(T[] arr) where T : IComparable<T>
        {
            return NextPermutation(arr, (a, b) => a.CompareTo(b));
        }

        public static bool NextPermutation<T>(T[] arr, Comparison<T> comp)
        {
            return NextPermutation(arr, 0, arr.Length, comp);
        }

        public static bool NextPermutation<T>(T[] arr, int start, int count, Comparison<T> comp)
        {
            if (count < 2) return false;

            for (int i = start + count - 2; i >= start; --i)
            {
                if (comp(arr[i], arr[i + 1]) < 0)
                {
                    for (int j = start + count - 1; ; --j)
                    {
                        if (comp(arr[i], arr[j]) < 0)
                        {
                            T tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
                            Array.Reverse(arr, i + 1, count - (i + 1 - start));
                            return true;
                        }
                    }
                }
            }

            Array.Reverse(arr, start, count);
            return false;
        }

        #region Template

        public static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            Thread main = new Thread(new ThreadStart(s_threadStart), 200 * 1024 * 1024);
            timer.Start();
            main.Start();
            main.Join();
            timer.Stop();
            if (s_time)
                Wl(timer.ElapsedMilliseconds);
        }

        private static IEnumerator<string> ioEnum;
        private static string GetString()
        {
            while (ioEnum == null || !ioEnum.MoveNext())
            {
                ioEnum = Console.ReadLine().Split().AsEnumerable().GetEnumerator();
            }

            return ioEnum.Current;
        }

        private static int GetInt()
        {
            return int.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static long GetLong()
        {
            return long.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static double GetDouble()
        {
            return double.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static List<int> GetIntArr(int n)
        {
            List<int> ret = new List<int>(n);
            for (int i = 0; i < n; i++)
            {
                ret.Add(GetInt());
            }
            return ret;
        }

        private static void Wl<T>(T o)
        {
            if (o is double)
            {
                Wld((o as double?).Value, "");
            }
            else if (o is float)
            {
                Wld((o as float?).Value, "");
            }
            else
                Console.WriteLine(o.ToString());
        }

        private static void Wl<T>(IEnumerable<T> enumerable)
        {
            Wl(string.Join(" ", enumerable.Select(e => e.ToString()).ToArray()));
        }

        private static void Wld(double d, string format)
        {
            Wl(d.ToString(format, CultureInfo.InvariantCulture));
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Collections;
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()
        {
            return double.Parse(ReadNextToken());
        }

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

        public void Write(double val, int precision)
        {
            Int64 valI = (Int64)val;
            writer.Write(valI.ToString());
            double diff = val - valI;
            if (diff > 0)
                writer.Write(".");
            int i;
            for (i = 0; i < precision; i++)
            {
                diff *= 10;
                writer.Write(((int)diff).ToString());
                diff -= (int)diff;
            }
        }

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

        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;
        int[] l;
        int[] r;
        double[] probThisResult;
        double[] EVThisResult;

        public void Solve()
        {
            n = ioHelper.ReadNextInt();
            l = new int[n];
            r = new int[n];

            probThisResult = new double[10002];
            EVThisResult = new double[10002];

            int i;
            for (i = 0; i < n; i++)
            {
                l[i] = ioHelper.ReadNextInt();
                r[i] = ioHelper.ReadNextInt();
            }

            int winningBid = 1;
            for (; winningBid <= 10000; winningBid++)
            {
                double probabilityNone = 1.0;
                double probabilityOne = 0;
                double probabilityAtLeastTwo = 0;

                for (int loser = 0; loser < n; loser++)
                {
                    double hisProbWinning = 1.0;
                    if (winningBid < l[loser] || winningBid > r[loser])
                        hisProbWinning = 0;

                    double hisProbLesserThan = (winningBid - l[loser]);
                    if (winningBid < l[loser])
                        hisProbLesserThan = 0.0;
                    if (winningBid > r[loser])
                        hisProbLesserThan = r[loser] - l[loser] + 1.0;

                    probabilityAtLeastTwo = probabilityOne * hisProbWinning + probabilityAtLeastTwo * (hisProbLesserThan + hisProbWinning);
                    probabilityOne = probabilityNone * hisProbWinning + probabilityOne * (hisProbLesserThan);
                    probabilityNone = probabilityNone * hisProbLesserThan;

                    probabilityAtLeastTwo /= (r[loser] - l[loser] + 1.0);
                    probabilityOne /= (r[loser] - l[loser] + 1.0);
                    probabilityNone /= (r[loser] - l[loser] + 1.0);
                }

                probThisResult[winningBid] += probabilityAtLeastTwo;
            }

            int secondBid = 1;
            for (; secondBid < 10001; secondBid++)
            {
                double probAtLeastOneNoWinner = 0;
                double probAtLeastOneWinner = 0;
                double probNoneNoWinner = 1.0;
                double probNoneOneWinner = 0.0;

                for (int loser = 0; loser < n; loser++)
                {
                    double hisProbSecond = 1.0;
                    if (secondBid < l[loser] || secondBid > r[loser])
                        hisProbSecond = 0;

                    double hisProbLesserThan = (secondBid - l[loser]);
                    if (secondBid < l[loser])
                        hisProbLesserThan = 0;
                    if (secondBid > r[loser])
                        hisProbLesserThan = 1.0 * (r[loser]-l[loser]+1.0);

                    double hisProbWinner = (r[loser] - secondBid);
                    if (secondBid + 1 > r[loser])
                        hisProbWinner = 0;
                    if (secondBid + 1 < l[loser])
                        hisProbWinner = 1.0 * (r[loser]-l[loser]+1.0);

                    double pnnw = probNoneNoWinner * hisProbLesserThan;
                    double pnw = probNoneNoWinner * hisProbWinner + probNoneOneWinner * (hisProbLesserThan);
                    double panw = probAtLeastOneNoWinner * (hisProbLesserThan + hisProbSecond) + probNoneNoWinner * (hisProbSecond);
                    double paw = probAtLeastOneWinner * (hisProbLesserThan+hisProbSecond) + probAtLeastOneNoWinner * hisProbWinner + probNoneOneWinner * hisProbSecond;

                    probNoneNoWinner = pnnw;
                    probNoneOneWinner = pnw;
                    probAtLeastOneNoWinner = panw;
                    probAtLeastOneWinner = paw;

                    probNoneNoWinner /= (r[loser] - l[loser] + 1.0);
                    probNoneOneWinner /= (r[loser] - l[loser] + 1.0);
                    probAtLeastOneNoWinner /= (r[loser] - l[loser] + 1.0);
                    probAtLeastOneWinner /= (r[loser] - l[loser] + 1.0);
                }

                probThisResult[secondBid] += probAtLeastOneWinner;
            }

            double result = 0;
            for (i = 0; i < 10001; i++)
                result += probThisResult[i] * i;

            ioHelper.Write(result, 10);
            ioHelper.WriteLine("");
            ioHelper.Flush();
        }

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


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

public class _513C {

    static int n;
    static int[] l, r;
    static double numerator = 0, denominator = 1;
    static int[] mask, tmp;
    static int second;

    public static void Main (string[] args) {

      var cin = new Tokenizer(Console.OpenStandardInput());
        var cout = new BufferedStdoutWriter ();

        n = int.Parse (cin.Next ());
        l = new int[n];
        r = new int[n];
        for (int i = 0; i < n; i++) {
            l [i] = int.Parse (cin.Next ());
            r [i] = int.Parse (cin.Next ());
            denominator *= (r [i] - l [i] + 1);
        }

        mask = new int[n];
        tmp = new int[n];

        for (second = 1; second <= 10000; second++)
            dfs (0);

        cout.WriteLine ("{0:F20}", numerator / denominator);
        cout.Close ();
    }

    static int calc (int l, int r, int ll, int rr) {
        if (ll > l)
            l = ll;
        if (rr < r)
            r = rr;
        return l <= r ? (r - l + 1) : 0;
    }

    static void dfs (int t) {
        if (t >= n) {
            for (int i = 0; i < n; i++)
                tmp [i] = mask [i];
            Array.Sort (tmp);
            if (tmp [n - 2] == 0) {
                double prod = 1;
                for (int i = 0; i < n; i++)
                    if (mask [i] == 1)
                        prod *= calc (l [i], r [i], second + 1, int.MaxValue);
                    else if (mask [i] == 0)
                        prod *= calc (l [i], r [i], second, second);
                    else
                        prod *= calc (l [i], r [i], 0, second - 1);
                numerator += second * prod;
            }
        } else {
            for (int i = -1; i <= 1; i++) {
                mask [t] = i;
                dfs (t + 1);
            }
        }
    }
}

namespace Kattis.IO {
    public class NoMoreTokensException : Exception {
    }

    public class Tokenizer {
        string[] tokens = new string[0];
        private int pos;
        StreamReader reader;

        public Tokenizer (Stream inStream) {
            var bs = new BufferedStream (inStream);
            reader = new StreamReader (bs);
        }

        public Tokenizer () : this (Console.OpenStandardInput ()) {
            // Nothing more to do
        }

        private string PeekNext () {
            if (pos < 0)
                // pos < 0 indicates that there are no more tokens
                return null;
            if (pos < tokens.Length) {
                if (tokens [pos].Length == 0) {
                    ++pos;
                    return PeekNext ();
                }
                return tokens [pos];
            }
            string line = reader.ReadLine ();
            if (line == null) {
                // There is no more data to read
                pos = -1;
                return null;
            }
            // Split the line that was read on white space characters
            tokens = line.Split (null);
            pos = 0;
            return PeekNext ();
        }

        public bool HasNext () {
            return (PeekNext () != null);
        }

        public string Next () {
            string next = PeekNext ();
            if (next == null)
                throw new NoMoreTokensException ();
            ++pos;
            return next;
        }
    }

    public class Scanner : Tokenizer {

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

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

        public float NextFloat () {
            return float.Parse (Next ());
        }

        public double NextDouble () {
            return double.Parse (Next ());
        }
    }

    public class BufferedStdoutWriter : StreamWriter {
        public BufferedStdoutWriter () : base (new BufferedStream (Console.OpenStandardOutput ())) {
        }
    }
}


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

namespace Codeforces
{
    class C
    {
        private static ThreadStart s_threadStart = new C().Go;
        private static bool s_time = false;
        private static double s_eps = 1e-16;

        int n;
        List<int[]> ranges;
        int[] t;

        double P(int x)
        {
            double p = 0.0;

            for (int i = 0; i < n; i++)
            {
                t[i] = i;
            }

            do
            {
                double xx = 1.0;

                //all equal
                if (n > 1)
                {
                    xx = 1.0;
                    for (int i = 0; i < n; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    p += xx;
                }

                //all but one equal
                if (n > 2)
                {
                    xx = 1.0;
                    for (int i = 0; i < n - 1; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    xx *= (1.0 - singleP(t[n - 1], x));
                    p += xx;
                }

                //all but 2 equal
                if (n > 3)
                {
                    xx = 1.0;
                    for (int i = 0; i < n - 2; i++)
                    {
                        xx *= singleP(t[i], x) / (i + 1);
                    }
                    xx *= (biggerP(t[n - 1], x) + smallerP(t[n - 1], x) * 0.5) * smallerP(t[n - 2], x);
                    p += xx;
                }

                //all but 3 equal = 2 are equal
                if (n == 5)
                {
                    xx = 0.5;
                    xx *= singleP(t[0], x);
                    xx *= singleP(t[1], x);
                    xx *= (biggerP(t[2], x) * 0.5 + smallerP(t[2], x) / 6) * smallerP(t[3], x) * smallerP(t[4], x);
                }

                //only one equal

                xx = singleP(t[0], x);
                xx *= biggerP(t[1], x);
                for (int i = 2; i < n; i++)
                {
                    xx *= smallerP(t[i], x) / (i - 1);
                }

                p += xx;

            } while (NextPermutation(t));

            return p;
        }

        double singleP(int i, int x)
        {
            if (x < ranges[i][0] || x > ranges[i][1]) return 0.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            return 1.0 / span;
        }

        double biggerP(int i, int x)
        {
            if (x > ranges[i][1]) return 0.0;
            if (x < ranges[i][0]) return 1.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            double num = ranges[i][1] - x;
            return num / span;
        }

        double smallerP(int i, int x)
        {
            if (x > ranges[i][1]) return 1.0;
            if (x < ranges[i][0]) return 0.0;
            double span = ranges[i][1] - ranges[i][0] + 1;
            double num = x - ranges[i][0];
            return num / span;
        }

        private void Go()
        {
            n = GetInt();
            ranges = new List<int[]>(n);
            t = new int[n];
            for (int i = 0; i < n; i++)
            {
                ranges.Add(new int[] { GetInt(), GetInt() });
            }

            double ans = 0.0;
            for (int i = 1; i <= 10000; i++)
            {
                ans += P(i) * i;
            }

            Wl(ans);
        }

        public static bool NextPermutation<T>(T[] arr) where T : IComparable<T>
        {
            return NextPermutation(arr, (a, b) => a.CompareTo(b));
        }

        public static bool NextPermutation<T>(T[] arr, Comparison<T> comp)
        {
            return NextPermutation(arr, 0, arr.Length, comp);
        }

        public static bool NextPermutation<T>(T[] arr, int start, int count, Comparison<T> comp)
        {
            if (count < 2) return false;

            for (int i = start + count - 2; i >= start; --i)
            {
                if (comp(arr[i], arr[i + 1]) < 0)
                {
                    for (int j = start + count - 1; ; --j)
                    {
                        if (comp(arr[i], arr[j]) < 0)
                        {
                            T tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp;
                            Array.Reverse(arr, i + 1, count - (i + 1 - start));
                            return true;
                        }
                    }
                }
            }

            Array.Reverse(arr, start, count);
            return false;
        }

        #region Template

        public static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            Thread main = new Thread(new ThreadStart(s_threadStart), 200 * 1024 * 1024);
            timer.Start();
            main.Start();
            main.Join();
            timer.Stop();
            if (s_time)
                Wl(timer.ElapsedMilliseconds);
        }

        private static IEnumerator<string> ioEnum;
        private static string GetString()
        {
            while (ioEnum == null || !ioEnum.MoveNext())
            {
                ioEnum = Console.ReadLine().Split().AsEnumerable().GetEnumerator();
            }

            return ioEnum.Current;
        }

        private static int GetInt()
        {
            return int.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static long GetLong()
        {
            return long.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static double GetDouble()
        {
            return double.Parse(GetString(), CultureInfo.InvariantCulture);
        }

        private static List<int> GetIntArr(int n)
        {
            List<int> ret = new List<int>(n);
            for (int i = 0; i < n; i++)
            {
                ret.Add(GetInt());
            }
            return ret;
        }

        private static void Wl<T>(T o)
        {
            if (o is double)
            {
                Wld((o as double?).Value, "");
            }
            else if (o is float)
            {
                Wld((o as float?).Value, "");
            }
            else
                Console.WriteLine(o.ToString());
        }

        private static void Wl<T>(IEnumerable<T> enumerable)
        {
            Wl(string.Join(" ", enumerable.Select(e => e.ToString()).ToArray()));
        }

        private static void Wld(double d, string format)
        {
            Wl(d.ToString(format, CultureInfo.InvariantCulture));
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Collections;
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()
        {
            return double.Parse(ReadNextToken());
        }

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

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

        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;
        int[] l;
        int[] r;
        double[] probThisResult;
        double[] EVThisResult;

        public void Solve()
        {
            n = ioHelper.ReadNextInt();
            l = new int[n];
            r = new int[n];

            probThisResult = new double[10002];
            EVThisResult = new double[10002];

            int i;
            for (i = 0; i < n; i++)
            {
                l[i] = ioHelper.ReadNextInt();
                r[i] = ioHelper.ReadNextInt();
            }

            int winningBid = 1;
            for (; winningBid <= 10000; winningBid++)
            {
                double probabilityNone = 1.0;
                double probabilityAtLeastOne = 0;
                double probabilityAtLeastTwo = 0;

                for (int loser = 0; loser < n; loser++)
                {
                    double hisProbWinning = 1.0 / (r[loser] - l[loser] + 1.0);
                    if (winningBid < l[loser] || winningBid > r[loser])
                        hisProbWinning = 0;

                    double hisProbLesserThan = (winningBid - l[loser]) / (r[loser] - l[loser] + 1.0);
                    if (winningBid < l[loser])
                        hisProbLesserThan = 0.0;
                    if (winningBid > r[loser])
                        hisProbLesserThan = 1.0;

                    probabilityAtLeastTwo = probabilityAtLeastOne * hisProbWinning + probabilityAtLeastTwo * (hisProbLesserThan + hisProbWinning);
                    probabilityAtLeastOne = probabilityNone * hisProbWinning + probabilityAtLeastOne * (hisProbLesserThan + hisProbWinning);
                    probabilityNone = probabilityNone * hisProbLesserThan;
                }

                if (probabilityAtLeastOne > 0)
                {
                    probThisResult[winningBid] += probabilityAtLeastTwo;
                }
            }

            int secondBid = 1;
            for (; secondBid < 10001; secondBid++)
            {
                double probAtLeastOneNoWinner = 0;
                double probAtLeastOneWinner = 0;
                double probNoneNoWinner = 1.0;
                double probNoneOneWinner = 0.0;

                for (int loser = 0; loser < n; loser++)
                {
                    double hisProbSecond = 1.0 / (r[loser] - l[loser] + 1.0);
                    if (secondBid < l[loser] || secondBid > r[loser])
                        hisProbSecond = 0;

                    double hisProbLesserThan = (secondBid - l[loser]) / (r[loser] - l[loser] + 1.0);
                    if (secondBid < l[loser])
                        hisProbLesserThan = 0;
                    if (secondBid > r[loser])
                        hisProbLesserThan = 1.0;

                    double hisProbWinner = (r[loser] - secondBid) / (r[loser] - l[loser] + 1.0);
                    if (secondBid + 1 > r[loser])
                        hisProbWinner = 0;
                    if (secondBid + 1 < l[loser])
                        hisProbWinner = 1.0;

                    double pnnw = probNoneNoWinner * hisProbLesserThan;
                    double pnw = probNoneNoWinner * hisProbWinner + probNoneOneWinner * (hisProbLesserThan);
                    double panw = probAtLeastOneNoWinner * (hisProbLesserThan + hisProbSecond) + probNoneNoWinner * (hisProbSecond);
                    double paw = probAtLeastOneWinner * (hisProbLesserThan + hisProbSecond) + probAtLeastOneNoWinner * hisProbWinner + probNoneOneWinner * hisProbSecond;

                    probNoneNoWinner = pnnw;
                    probNoneOneWinner = pnw;
                    probAtLeastOneNoWinner = panw;
                    probAtLeastOneWinner = paw;
                }

                probThisResult[secondBid] = probAtLeastOneWinner;
            }

            double result = 0;
            for (i = 0; i < 10001; i++)
                result += probThisResult[i] * i;

            Console.WriteLine(result.ToString("F"));

        }

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


using System;
using System.Linq;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;
namespace Program
{
    public class Solver
    {

        public void Solve()
        {
            var n = sc.Integer();
            var l = new int[n];
            var r = new int[n];
            var dp = new double[10001];
            const int max = 10;
            for (int i = 0; i < n; i++)
            {
                l[i] = sc.Integer();
                r[i] = sc.Integer();
            }
            {
                var other = new double[max];
                for (int i = l[0]; i <= r[0]; i++)
                    for (int j = l[1]; j <= r[1]; j++)
                        for (int k = l[2]; k <= r[2]; k++)
                        {
                            var v = new int[] { i, j, k };

                        }

            }

            for (int v = 1; v <= max; v++)
            {
                for (int top = -1; top < n; top++)
                {
                    for (int sec = 1; sec < 1 << n; sec++)
                    {
                        if (top == -1)
                        {
                            var count = 0;
                            for (int k = 0; k < n; k++)
                                if ((sec >> k) % 2 == 1) count++;
                            if (count == 1) continue;
                        }
                        var f = new int[n];
                        var flag = true;
                        for (int k = 0; k < n; k++)
                        {
                            if (top == k)
                            {
                                if (r[k] <= v)
                                {
                                    flag = false; break;
                                }
                                f[k] |= 1;
                            }
                            if ((sec >> k) % 2 == 1)
                            {
                                if (v < l[k] || r[k] < v)
                                {
                                    flag = false; break;
                                }
                                f[k] |= 2;
                            }
                            if (f[k] == 3)
                            {
                                flag = false; break;
                            }
                            if (f[k] == 0)
                            {
                                if (v <= l[k])
                                {
                                    flag = false; break;
                                }
                            }
                        }

                        if (!flag) continue;
                        var p = 1.0;
                        for (int k = 0; k < n; k++)
                        {
                            if (top == k)
                            {
                                p *= 1.0 * (r[k] - Math.Max(l[k], v + 1) + 1) / (r[k] - l[k] + 1);
                            }
                            else if ((sec >> k) % 2 == 1)
                            {
                                p *= 1.0 / (r[k] - l[k] + 1);
                            }
                            else
                            {
                                p *= 1.0 * (Math.Min(r[k], v - 1) - l[k] + 1) / (r[k] - l[k] + 1);
                            }

                        }
                        //Debug.WriteLine("{0}: top:{1} sec:{2} p:{3} ", v, top, sec, p);
                        dp[v] += p;
                    }
                }
            }
            var ans = 0.0;
            for (int i = 0; i <= 10000; i++)
            {
                ans += i * dp[i];
            }
            IO.Printer.Out.WriteLine(ans);
        }
        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }

    }

}

#region Ex
namespace Program.IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;
    public class Printer : StreamWriter
    {
        static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
        public static Printer Out { get; set; }
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
        public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
        public void Write<T>(string format, IEnumerable<T> source) { base.Write(format, source.OfType<object>().ToArray()); }
        public void WriteLine<T>(string format, IEnumerable<T> source) { base.WriteLine(format, source.OfType<object>().ToArray()); }
    }
    public class StreamScanner
    {
        public StreamScanner(Stream stream) { str = stream; }
        private readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read()
        {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while (b < 33 || 126 < b);            return (char)b; }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public int Integer() { return (isEof) ? int.MinValue : (int)Long(); }
        public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n) { return enumerate(n, Char); }
        public string[] Scan(int n) { return enumerate(n, Scan); }
        public double[] Double(int n) { return enumerate(n, Double); }
        public int[] Integer(int n) { return enumerate(n, Integer); }
        public long[] Long(int n) { return enumerate(n, Long); }
    }
}
static class Ex
{
    static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
    static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); }
    static public void Main()
    {
        var solver = new Program.Solver();
        solver.Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion


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

namespace cf
{
    class Program
    {
        private int n;
        private int k;
        private int[] nums;
        private int startIndex;
        private int total;

        void searchLuckNumber(uint now)
        {
            if (now <= n)
            {
                if (isLuckNumber(getNum(now))) total++;

                searchLuckNumber(now * 10 + 4);
                searchLuckNumber(now * 10 + 7);
            }
        }

        uint getNum(uint index)
        {
            if (index < startIndex)
            {
                return index;
            }
            return (uint)nums[index - startIndex];
        }

        static Boolean isLuckNumber(uint num)
        {
            string str = string.Format("{0}", num);
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != '4' && str[i] != '7')
                {
                    return false;
                }
            }
            return true;
        }

        void solve()
        {
            int[] f = new int[16];
            f[0] = 1;
            for (int i = 1; i <= 15; i++)
            {
                f[i] = f[i - 1] * i;
            }

            string nk = Console.ReadLine();
            string[] nks = nk.Split(' ');
            n = int.Parse(nks[0]);
            k = int.Parse(nks[1]);
            int orin = n;
            int orik = k;

            n = Math.Min(n, 15);
            startIndex = orin - n + 1;
            nums = new int[n];
            bool[] used = new bool[n + 1];
            int based = orin - n;
            for (int i = 0; i < n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (used[j]) continue;
                    if (k > f[n - 1 - i])
                    {
                        k -= f[n - 1 - i];
                    }
                    else
                    {
                        used[j] = true;
                        nums[i] = based + j;
                        break;
                    }
                }
            }

            k = orik;
            n = orin;

            searchLuckNumber(4);
            searchLuckNumber(7);

            Console.WriteLine(total);
        }

        static void Main(string[] args)
        {
            Program m = new Program();
            m.solve();
        }
    }
}


using System;
using System.Collections.Generic;

class MainClass{
        int fact(int x)
        {
            return x == 0 ? 1 : fact(x - 1) * x;
        }

        int pow(int x, int y)
        {
            return y == 0 ? 1 : pow(x, y - 1) * x;
        }
        void gen47s(List<int> arr, int piv, int bias, int n)
        {
            if (piv == 0)
            {
                if (bias + 4 <= n)
                    arr.Add(bias + 4);
                if (bias + 7 <= n)
                    arr.Add(bias + 7);
            }
            else
            {
                gen47s(arr, piv - 1, bias + 4 * pow(10, piv), n);
                gen47s(arr, piv - 1, bias + 7 * pow(10, piv), n);
            }
        }

        int main(int n, int k)
        {
            int ans = 0;
            --k;
            if (n <= 12 && k >= fact(n))
            {
                ans = -1;
                return ans;
            }
            int cons = 12;
            for (cons = 12; cons > 0; --cons)
                if (k >= fact(cons))
                    break;
            List<int> rest = new List<int>();
            for (int i = n - cons; i <= n; ++i)
                rest.Add(i);
            List<int> arr47 = new List<int>();
            for (int i = 0; i < ("" + n).Length; ++i)
                gen47s(arr47, i, 0, n);
            for (int i = arr47.Count - 1; i >= 0; --i)
                if (arr47[i] < n - cons)
                {
                    ans = i + 1;
                    break;
                }
            foreach (int i in rest.ToArray())
            {
                int q = k / fact(n - i);
                int current = rest[q];
                rest.Remove(current);
                if (arr47.Contains(i) && arr47.Contains(current))
                    ans++;
                k -= q * fact(n - i);
            }
            return ans;
        }
        static void Main(){
            int n, k;
            string[] items = Console.ReadLine().Split(' ');
            n = int.Parse(items[0]);
            k = int.Parse(items[1]);
            MainClass mn = new MainClass();
            Console.Write(mn.main(n, k));
        }
}


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

namespace _LuckyPermutation
{
    public class Puzzle
    {
        private static readonly long[] Factorials =
            new[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800 };

        public static int[] FindPerm(int n, int k)
        {
            if (k < 1) return null;
            if (n > 13) return null;
            if (k > Factorials[n]) return null;

            var perm = new List<int> { 0 };

            var pos = 1L;
            for (var i = n; i > 0; i--)
            {
                var f = Factorials[i - 1];
                for (var j = 1; j <= n; j++)
                {
                    if (perm.Contains(j)) continue;

                    if (pos + f > k)
                    {
                        perm.Add(j);
                        break;
                    }
                    pos += f;
                }
            }

            return perm.ToArray();
        }
    }

    class ProgramLuckyPermutation
    {
        static void Main(/*string[] args*/)
        {
            var result = Console.ReadLine();
            if (result == null) return;
            var strnk = result.Split();
            var n = Convert.ToInt32(strnk[0]);  // permutation of integers between 1 and n
            var k = Convert.ToInt32(strnk[1]);  // lexicographical number of the permutation

            var seq = Puzzle.FindPerm(n, k);

            if (seq == null) Console.WriteLine("-1");
            else
            {
                var count = 0;
                for (var j = 1; j < seq.Count(); j++)
                {
                    if ((j==4 || j==7) && (seq[j] == 4 || seq[j] == 7))
                    {
                        count++;
                    }
                    //Console.Write("{0},", seq[j]);
                }
                Console.WriteLine(count);
            }
            //Console.WriteLine();
        }
    }
}


using System;
using System.Collections.Generic;

class MainClass{
        int fact(int x)
        {
            return x == 0 ? 1 : fact(x - 1) * x;
        }

        long pow(int x, int y)
        {
            return y == 0 ? 1 : pow(x, y - 1) * x;
        }
        void gen47s(List<int> arr, int piv, long bias, int n)
        {
            if (piv == 0)
            {
                if (bias + 4 <= n)
                    arr.Add((int)(bias + 4));
                if (bias + 7 <= n)
                    arr.Add((int)(bias + 7));
            }
            else
            {
                gen47s(arr, piv - 1, bias + 4 * pow(10, piv), n);
                gen47s(arr, piv - 1, bias + 7 * pow(10, piv), n);
            }
        }
        int main(int n, int k)
        {
            int ans = 0;
            --k;
            if (n <= 12 && k >= fact(n))
            {
                ans = -1;
                return ans;
            }
            int cons = 12;
            for (cons = 12; cons > 0; --cons)
                if (k >= fact(cons))
                    break;
            List<int> rest = new List<int>();
            for (int i = n - cons; i <= n; ++i)
                rest.Add(i);
            List<int> arr47 = new List<int>();
            for (int i = 0; i < ("" + n).Length; ++i)
                gen47s(arr47, i, 0, n);
            for (int i = arr47.Count - 1; i >= 0; --i)
                if (arr47[i] < n - cons)
                {
                    ans = i + 1;
                    break;
                }
            foreach (int i in rest.ToArray())
            {
                int q = k / fact(n - i);
                int current = rest[q];
                rest.Remove(current);
                if (arr47.Contains(i) && arr47.Contains(current))
                    ans++;
                k -= q * fact(n - i);
            }
            return ans;
        }
        static void Main(){
            int n, k;
            var items = Console.ReadLine().Split(' ');
            n = int.Parse(items[0]);
            k = int.Parse(items[1]);
            MainClass mn = new MainClass();
            Console.Write(mn.main(n, k));
        }
}


using System;
using System.Collections.Generic;

class MainClass{
        int fact(int x)
        {
            return x == 0 ? 1 : fact(x - 1) * x;
        }

        long pow(int x, int y)
        {
            return y == 0 ? 1 : pow(x, y - 1) * x;
        }
        void gen47s(List<int> arr, int piv, long bias, int n)
        {
            if (piv == 0)
            {
                if (bias + 4 <= n)
                    arr.Add((int)(bias + 4));
                if (bias + 7 <= n)
                    arr.Add((int)(bias + 7));
            }
            else
            {
                gen47s(arr, piv - 1, bias + 4 * pow(10, piv), n);
                gen47s(arr, piv - 1, bias + 7 * pow(10, piv), n);
            }
        }
        int main(int n, int k)
        {
            int ans = 0;
            --k;
            if (n <= 12 && k >= fact(n))
            {
                ans = -1;
                return ans;
            }
            int cons = 12;
            for (cons = 12; cons > 0; --cons)
                if (k >= fact(cons))
                    break;
            List<int> rest = new List<int>();
            for (int i = n - cons; i <= n; ++i)
                rest.Add(i);
            List<int> arr47 = new List<int>();
            for (int i = 0; i < ("" + n).Length; ++i)
                gen47s(arr47, i, 0, n);
            for (int i = arr47.Count - 1; i >= 0; --i)
                if (arr47[i] < n - cons)
                {
                    ans = i + 1;
                    break;
                }
            foreach (int i in rest.ToArray())
            {
                int q = k / fact(n - i);
                int current = rest[q];
                rest.Remove(current);
                if (arr47.Contains(i) && arr47.Contains(current))
                    ans++;
                k -= q * fact(n - i);
            }
            return ans;
        }
        static void Main(){
            int n, k;
            string[] items = Console.ReadLine().Split(' ');
            n = int.Parse(items[0]);
            k = int.Parse(items[1]);
            MainClass mn = new MainClass();
            Console.Write(mn.main(n, k));
        }
}


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

namespace Task
{
    class Program
    {
        int getCount(int curNum, int maxNum)
        {
            int res = curNum == 0 ? 0 : 1;
            if (curNum * 10L + 4 <= maxNum)
                res += getCount(curNum * 10 + 4, maxNum);
            if (curNum * 10L + 7 <= maxNum)
                res += getCount(curNum * 10 + 7, maxNum);
            return res;
        }

        void swap(int[] arr, int a, int b)
        {
            int temp = arr[a];
            arr[a] = arr[b];
            arr[b] = temp;
        }

        void permute(int[] array, int k)
        {
            while (k > 1)
            {
                int curFact = 1;
                int prevFact = 1;
                int pos = 1;
                while (curFact < k)
                {
                    pos++;
                    prevFact = curFact;
                    curFact *= pos;
                }
                int absPos0 = array.Length - pos;
                int shift = (k - 1) / prevFact;
                int absPos1 = absPos0 + shift;
                swap(array, absPos0, absPos1);
                if (pos > 1)
                {
                    Array.Sort(array, absPos0 + 1, pos - 1);
                }

                k -= prevFact * shift;
            }
        }

        bool isLucky(int x)
        {
            while (x > 0)
            {
                if (x % 10 != 4 && x % 10 != 7)
                    return false;
                x /= 10;
            }
            return true;
        }

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

            long fact = 1;
            int numCount = 1;
            for (int i =  2; i <= n && fact <= k; i++)
            {
                fact *= i;
                numCount++;
            }

            if (fact < k)
            {
                io.Print(-1);
                return;
            }

            int firstCnt = n - numCount;

            int ans = getCount(0, firstCnt);
            int[] lastNumbers = new int[numCount];
            for (int i = 0; i < numCount; i++)
                lastNumbers[i] = n - numCount + 1 + i;

            permute(lastNumbers, k);
            for (int i = 0; i < lastNumbers.Length; i++)
                if (isLucky(firstCnt + i + 1) && isLucky(lastNumbers[i]))
                {
                    ans++;
                }

            io.Print(ans);
        }

        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 cf
{
    class Program
    {
        private int n;
        private int k;
        private int[] nums;
        private int startIndex;
        private int total;

        void searchLuckNumber(uint now)
        {
            if (now <= n)
            {
                if (isLuckNumber(getNum(now))) total++;

                searchLuckNumber(now * 10 + 4);
                searchLuckNumber(now * 10 + 7);
            }
        }

        uint getNum(uint index)
        {
            if (index < startIndex)
            {
                return index;
            }
            return (uint)nums[index - startIndex];
        }

        static Boolean isLuckNumber(uint num)
        {
            string str = string.Format("{0}", num);
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != '4' && str[i] != '7')
                {
                    return false;
                }
            }
            return true;
        }

        void solve()
        {
            int[] f = new int[16];
            f[0] = 1;
            for (int i = 1; i <= 15; i++)
            {
                f[i] = f[i - 1] * i;
            }

            string nk = Console.ReadLine();
            string[] nks = nk.Split(' ');
            n = int.Parse(nks[0]);
            k = int.Parse(nks[1]);
            int orin = n;
            int orik = k;

            n = Math.Min(n, 15);
            startIndex = orin - n + 1;
            nums = new int[n];
            bool[] used = new bool[n + 1];
            int based = orin - n;

            if (k > f[n]) {
                Console.WriteLine("-1");
                return;
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (used[j]) continue;
                    if (k > f[n - 1 - i])
                    {
                        k -= f[n - 1 - i];
                    }
                    else
                    {
                        used[j] = true;
                        nums[i] = based + j;
                        break;
                    }
                }
            }

            k = orik;
            n = orin;

            searchLuckNumber(4);
            searchLuckNumber(7);

            Console.WriteLine(total);
        }

        static void Main(string[] args)
        {
            Program m = new Program();
            m.solve();
        }
    }
}


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

namespace ProgrammingContest.Codeforces.Round91 {
    class C {
        static List<int> num = new List<int>();
        static void gen(long x, int n) {
            if (x <= n) {
                if(x != 0)
                    num.Add((int)x);
                gen(x * 10 + 4, n);
                gen(x * 10 + 7, n);
            }
        }

        static long fact(long n) { long res = 1; while (n > 1) res *= n--; return res; }

        static int n;
        static List<int> val = new List<int>();
        static int dfs(int pos, long k) {
            if (pos > n) return 0;
            int rest = n - pos,
                res = 0;
            for (int i = 0; i < val.Count; i++) {
                if (val[i] == -1) continue;
                if (k < fact(rest)) {
                    if (num.Contains(val[i]) && num.Contains(pos)) {
                        res = 1;
                    }
                    //Console.WriteLine("dfs: {0} {1}", pos, val[i]);
                    val[i] = -1;
                    return res + dfs(pos + 1, k);
                }
                k -= fact(rest);
            }
            throw new Exception();
        }

        static void Main() {
            var xs = Array.ConvertAll<string, int>(Console.ReadLine().Split(' '), int.Parse);
            n = xs[0];
            int k = xs[1]-1;

            for (int v = k, i = 1; i <= n && v != 0; i++) {
                v /= i;
                if (i == n && v != 0) {
                    Console.WriteLine(-1);
                    return;
                }
            }

            gen(0, n);
            num.Sort();
            for (long v = 1, i = 1; i <= n; i++) {
                v *= i;
                if (k < v) {
                    int res = 0;
                    int m = (int)(n - i);
                    int id = -1;
                    //Console.WriteLine("dfs: {0} {1}", m, res);
                    for (int j = 0; j < num.Count; j++) {
                        if (num[j] > m) {
                            res += j;
                            id = j;
                            break;
                        }
                        if (j == num.Count - 1) {
                            Console.WriteLine(num.Count);
                            return;
                        }
                    }
                    //Console.WriteLine("dfs: {0} {1}", m, res);
                    for (int j = m; j < n; j++) {
                        val.Add(j + 1);
                    }
                    Console.WriteLine(dfs(m+1, k) + res);
                    return;
                }
            }
        }
    }
}


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

namespace _LuckyPermutation
{
    public class Puzzle
    {
        private static readonly List<long> Lucky = GetLuckyNumbers(10);
        private static int _n;

        static List<long> GetLuckyNumbers(int length)
        {
            var strNums = new List<string> { "4", "7" };
            var prevCount = 0;
            while (strNums[strNums.Count - 1].Length < length)
            {
                var count = strNums.Count;
                for (var i = prevCount; i < count; i++)
                {
                    strNums.Add(strNums[i] + "4");
                    strNums.Add(strNums[i] + "7");
                }
                prevCount = count;
            }

            return strNums.Select(long.Parse).ToList();
        }

        private static readonly long[] Factorials =
            new[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800 };

        public static int[] FindPerm(int n, int k, out int nseq)
        {
            nseq = n;   // number of non-sequential elements
            _n = n;
            if (k < 1 || (n < Factorials.Count() && k > Factorials[n]) || k > 1000000000) return null;
            var perm = new HashSet<int> { 0 };

            if (n > 13)
            {
                nseq = 0;
                for (var i=1; i<Factorials.Count(); i++)
                {
                    //if (k < Factorials[i] || k >= Factorials[i + 1]) continue;
                    if (k >= Factorials[i + 1]) continue;
                    nseq = i + 1;
                    break;
                }
/*
                for (var i=1; i<=n-nseq; i++)
                {
                    perm.Add(i);
                }
*/

            }

            var pos = 1L;
            for (var i = nseq; i > 0; i--)
            {
                var f = (i > Factorials.Count()) ? Factorials[Factorials.Count() - 1] : Factorials[i - 1];
                for (var j = (n - nseq + 1); j <= n; j++)
                {
                    if (perm.Contains(j)) continue;

                    if (pos + f > k)
                    {
                        perm.Add(j);
                        break;
                    }
                    pos += f;
                }
            }

            return perm.ToArray();
        }

        public static int CountLucky(int[] seq, int nseq)
        {
            if (seq == null) return -1;

            var count = 0;
            var seqCount = _n - nseq;
            //var seqCount = seq.Count() - nseq;

            var i = 0;
            while (Lucky[i++] <= seqCount) count++;

            for (var j = seqCount; j <= _n; j++)
            {
                if (Lucky.Contains(j) && Lucky.Contains(seq[j - seqCount]))
                {
                    count++;
                }
                //Console.Write("{0},", seq[j]);
            }
            //Console.WriteLine(count);
            //Console.WriteLine();

            return count;
        }
    }

    class ProgramLuckyPermutation
    {
        static void Main(/*string[] args*/)
        {
            var result = Console.ReadLine();
            if (result == null) return;
            var strnk = result.Split();
            var n = Convert.ToInt32(strnk[0]);  // permutation of integers between 1 and n
            var k = Convert.ToInt32(strnk[1]);  // lexicographical number of the permutation

            int nseq;
            var seq = Puzzle.FindPerm(n, k, out nseq);
            Console.WriteLine(Puzzle.CountLucky(seq, nseq));
        }
    }
}


class MainClass{
        int fact(int x)
        {
            return x == 0 ? 1 : fact(x - 1) * x;
        }

        int pow(int x, int y)
        {
            return y == 0 ? 1 : pow(x, y - 1) * x;
        }
        void gen47s(List<int> arr, int piv, int bias, int n)
        {
            if (piv == 0)
            {
                if (bias + 4 <= n)
                    arr.Add(bias + 4);
                if (bias + 7 <= n)
                    arr.Add(bias + 7);
            }
            else
            {
                gen47s(arr, piv - 1, bias + 4 * pow(10, piv), n);
                gen47s(arr, piv - 1, bias + 7 * pow(10, piv), n);
            }
        }

        int main(int n, int k)
        {
            Debug.Assert(n < 1e9 && k < 1e9);
            int ans = 0;
            --k;
            if (n <= 12 && k >= fact(n))
            {
                ans = -1;
                return ans;
            }
            int cons = 12;
            for (cons = 12; cons >= 0; --cons)
                if (k >= fact(cons))
                    break;
            List<int> rest = new List<int>();
            for (int i = n - cons; i <= n; ++i)
                rest.Add(i);
            List<int> arr47 = new List<int>();
            for (int i = 0; i < ("" + n).Length; ++i)
                gen47s(arr47, i, 0, n);
            for (int i = arr47.Count - 1; i >= 0; --i)
                if (arr47[i] < n - cons)
                {
                    ans = i + 1;
                    break;
                }
            foreach (int i in rest.ToArray())
            {
                int q = k / fact(n - i);
                int current = rest[q];
                rest.Remove(current);
                if (arr47.Contains(i) && arr47.Contains(current))
                    ans++;
                k -= q * fact(n - i);
            }
            return ans;
        }
static void Main(){
int n, k;
string[] items = Console.ReadLine().Split(" ");
n = int.Parse(items[0]);
k = int.Parse(items[1]);

Console.Write(main(n, k));
}
}


using System;
using System.Collections.Generic;

class MainClass{
        int fact(int x)
        {
            return x == 0 ? 1 : fact(x - 1) * x;
        }

        int pow(int x, int y)
        {
            return y == 0 ? 1 : pow(x, y - 1) * x;
        }
        void gen47s(List<int> arr, int piv, int bias, int n)
        {
            if (piv == 0)
            {
                if (bias + 4 <= n)
                    arr.Add(bias + 4);
                if (bias + 7 <= n)
                    arr.Add(bias + 7);
            }
            else
            {
                gen47s(arr, piv - 1, bias + 4 * pow(10, piv), n);
                gen47s(arr, piv - 1, bias + 7 * pow(10, piv), n);
            }
        }

        int main(int n, int k)
        {
            int ans = 0;
            --k;
            if (n <= 12 && k >= fact(n))
            {
                ans = -1;
                return ans;
            }
            int cons = 12;
            for (cons = 12; cons >= 0; --cons)
                if (k >= fact(cons))
                    break;
            List<int> rest = new List<int>();
            for (int i = n - cons; i <= n; ++i)
                rest.Add(i);
            List<int> arr47 = new List<int>();
            for (int i = 0; i < ("" + n).Length; ++i)
                gen47s(arr47, i, 0, n);
            for (int i = arr47.Count - 1; i >= 0; --i)
                if (arr47[i] < n - cons)
                {
                    ans = i + 1;
                    break;
                }
            foreach (int i in rest.ToArray())
            {
                int q = k / fact(n - i);
                int current = rest[q];
                rest.Remove(current);
                if (arr47.Contains(i) && arr47.Contains(current))
                    ans++;
                k -= q * fact(n - i);
            }
            return ans;
        }
        static void Main(){
            int n, k;
            string[] items = Console.ReadLine().Split(' ');
            n = int.Parse(items[0]);
            k = int.Parse(items[1]);

            Console.Write(main(n, k));
        }
}


using System;
using System.Collections.Generic;

class MainClass{
        int fact(int x)
        {
            return x == 0 ? 1 : fact(x - 1) * x;
        }

        int pow(int x, int y)
        {
            return y == 0 ? 1 : pow(x, y - 1) * x;
        }
        void gen47s(List<int> arr, int piv, int bias, int n)
        {
            if (piv == 0)
            {
                if (bias + 4 <= n)
                    arr.Add(bias + 4);
                if (bias + 7 <= n)
                    arr.Add(bias + 7);
            }
            else
            {
                gen47s(arr, piv - 1, bias + 4 * pow(10, piv), n);
                gen47s(arr, piv - 1, bias + 7 * pow(10, piv), n);
            }
        }

        int main(int n, int k)
        {
            int ans = 0;
            --k;
            if (n <= 12 && k >= fact(n))
            {
                ans = -1;
                return ans;
            }
            int cons = 12;
            for (cons = 12; cons >= 0; --cons)
                if (k >= fact(cons))
                    break;
            List<int> rest = new List<int>();
            for (int i = n - cons; i <= n; ++i)
                rest.Add(i);
            List<int> arr47 = new List<int>();
            for (int i = 0; i < ("" + n).Length; ++i)
                gen47s(arr47, i, 0, n);
            for (int i = arr47.Count - 1; i >= 0; --i)
                if (arr47[i] < n - cons)
                {
                    ans = i + 1;
                    break;
                }
            foreach (int i in rest.ToArray())
            {
                int q = k / fact(n - i);
                int current = rest[q];
                rest.Remove(current);
                if (arr47.Contains(i) && arr47.Contains(current))
                    ans++;
                k -= q * fact(n - i);
            }
            return ans;
        }
        static void Main(){
            int n, k;
            string[] items = Console.ReadLine().Split(" ");
            n = int.Parse(items[0]);
            k = int.Parse(items[1]);

            Console.Write(main(n, k));
        }
}