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

namespace ConsoleApp15
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] nr = new int[n + 1];
            int[] nk = new int[n + 1];
            for (int i = 1; i <= n; i++)
            {
                string s;
                s = Console.ReadLine();
                string[] words = s.Split(' ');
                foreach (var word in words)
                {
                    nr[i] = int.Parse(words[0]);
                    nk[i] = int.Parse(words[1]);
                }
            }
            int[] b = new int[n + 1];
            
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (nr[i] == j) b[i]++;
                    if (nk[i] == j) b[i]++;
                }
                
            }
            

            int[] br = new int[n + 1];
            int[] bk = new int[n + 1];
            
            for (int i = 1; i < n + 1; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (nr[i] == j) br[j]++;
                    if (nk[i] == j) bk[j]++;
                }
                
            }
            int r = 1;
            int k = 1;
            int[] kr = new int[n + 1];
            int[] kk = new int[n + 1];
            for (int i = 1; i <= n; i++)
            {
              
                if (br[i] !=1) r = 0;
                if (bk[i] != 1) k = 0;
                kk[i] = 1;
                kr[i] = 1;
            }
            int p = 0;
            int p1 = 0;
           
            if (r == 1 && k == 1) Console.WriteLine(0);
            else if (r == 1 && k == 0)
            {
                for (int i = 1; i <= n; i++)
                {
                    if (bk[i] == 0) kk[i] = 0;
                    if (bk[i] > 1) kk[i] = 0;
                }
                for (int i = 1; i < n; i++)
                {
                    if (kk[i] == 0) { p = i; break; }
                }
                for (int i = n; i > 0; i--)
                {
                    if (kk[i] == 0) { p1 = i; break; }
                }
                Console.WriteLine(p1 - p);
            }
            else if (r == 0 && k == 1)
            {
                for (int i = 1; i <= n; i++)
                {
                    if (br[i] == 0) kr[i] = 0;
                    if (br[i] > 1) kr[i] = 0;
                }
                for (int i = 1; i < n; i++)
                {
                    if (kr[i] == 0) { p = i; break; }
                }
                for (int i = n; i > 0; i--)
                {
                    if (kr[i] == 0) { p1 = i; break; }
                }
                Console.WriteLine(p1 - p);
            }
            else if(r==0 && k==0)
            {
                for (int i = 1; i <= n; i++)
                {
                    if (bk[i] == 0) kk[i] = 0;
                    if (br[i] == 0) kr[i] = 0;
                    if (bk[i] > 1) kk[i] = 0;
                    if (br[i] > 1) kr[i] = 0;
                }
                for (int i = 1; i < n; i++)
                {
                    if (kk[i] == 0) { p = i; break; }
                }
                for (int i = n; i > 0; i--)
                {
                    if (kk[i] == 0) { p1 = i; break; }
                }
                int w=p1 - p;
                p1 = 0;
                p = 0;
                for (int i = 1; i < n; i++)
                {
                    if (kr[i] == 0) { p = i; break; }
                }
                for (int i = n; i > 0; i--)
                {
                    if (kr[i] == 0) { p1 = i; break; }
                }
                int w1=p1 - p;
                w1 = w1 + w;
                Console.WriteLine(w1);
            }

        }
    }
}