using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    ReadData re;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        re = new ReadData();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = re.i();
        int[] A = re.ia();
        int count = 0;
        count = 2*A[N-1]-A[N-2];
        for(int i=1;i<N-1;i++){
            if(A[i+1]-A[i] != A[1]-A[0]){
                count = A[N-1];
            }
        }
        sb.Append(count+"\n");
    }
}
class ReadData{
    string[] str;
    int counter;
    public ReadData(){
        counter = 0;
    }
    public string s(){
        if(counter == 0){
            str = Console.ReadLine().Split(' ');
            counter = str.Length;
        }
        counter--;
        return str[str.Length-counter-1];
    }
    public int i(){
        return int.Parse(s());
    }
    public long l(){
        return long.Parse(s());
    }
    public double d(){
        return double.Parse(s());
    }
    public int[] ia(int N){
        int[] ans = new int[N];
        for(int j=0;j<N;j++){
            ans[j] = i();
        }
        return ans;
    }
    public int[] ia(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        int[] ans = new int[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = int.Parse(str[j]);
        }
        return ans;
    }
    public long[] la(int N){
        long[] ans = new long[N];
        for(int j=0;j<N;j++){
            ans[j] = l();
        }
        return ans;
    }
    public long[] la(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        long[] ans = new long[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = long.Parse(str[j]);
        }
        return ans;
    }
    public double[] da(int N){
        double[] ans = new double[N];
        for(int j=0;j<N;j++){
            ans[j] = d();
        }
        return ans;
    }
    public double[] da(){
        str = Console.ReadLine().Split(' ');
        counter = 0;
        double[] ans = new double[str.Length];
        for(int j=0;j<str.Length;j++){
            ans[j] = double.Parse(str[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int[] f,int[] t){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<f.Length;j++){
            ans[f[j]].Add(t[j]);
            ans[t[j]].Add(f[j]);
        }
        return ans;
    }
    public List<int>[] g(int N,int M){
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
    public List<int>[] g(){
        int N = i();
        int M = i();
        List<int>[] ans = new List<int>[N];
        for(int j=0;j<N;j++){
            ans[j] = new List<int>();
        }
        for(int j=0;j<M;j++){
            int f = i()-1;
            int t = i()-1;
            ans[f].Add(t);
            ans[t].Add(f);
        }
        return ans;
    }
}
public static class Define{
    public const long mod = 1000000007;
}
public static class Debug{
    public static void Print(double[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(double[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(long[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(long[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
    public static void Print(int[,,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                for(int l=0;l<k.GetLength(2);l++){
                    Console.Write(k[i,j,l]+" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[,] k){
        for(int i=0;i<k.GetLength(0);i++){
            for(int j=0;j<k.GetLength(1);j++){
                Console.Write(k[i,j]+" ");
            }
            Console.WriteLine();
        }
    }
    public static void Print(int[] k){
        for(int i=0;i<k.Length;i++){
            Console.WriteLine(k[i]);
        }
    }
}


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

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            int b;

            int n = Convert.ToInt32(Console.ReadLine());

            int[] temp = new int[n];

            string[] tempStr = new string[n];

            string str = Console.ReadLine();

            char[] split = { ' ' };

            tempStr = str.Split(split);

            for (int k = 0; k <= n - 1; k++)
            {
                temp[k] = Convert.ToInt32(tempStr[k]);
            }

            int m = temp[0] - temp[1];
            b = 0;

            for (int k = 0; k <= n - 2; k++)
            {
                if (temp[k] - temp[k + 1] == m)
                {
                    b = b + 1;
                }
            }

            if (n > 2)
            {
                if (b == n - 1 && temp[n - 1] - temp[n - 2] == temp[n - 2] - temp[n - 3])
                {
                    Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
                }
                else
                {
                    Console.WriteLine(temp[n - 1]);
                }
            }
            else
            {
                Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
            }
        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            string[] y = Console.ReadLine().Split(' ');
            int a = int.Parse(y[0]) - int.Parse(y[1]);
            if (y.Length == 2)
            {
                if (int.Parse(y[0]) + a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[0]) - a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
            else
            {

                if (int.Parse(y[y.Length - 1]) - a * y.Length == int.Parse(y[0]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[y.Length - 1]) + a * y.Length == int.Parse(y[0]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
            //            int a = int.Parse(y[0]);
            //            int b = int.Parse(y[1]);
            //            //int max = -99999999;
            //            int k = 0;
            //            string c = Console.ReadLine();
            //            char[]v=new char[c.Length];
            //                        for (int i = 0; i < a; i++)
            //            {
            //v[i]=c[i];
            //            }
            //            //int i = 0;
            //            int h2 = 0;
            //            int h5 = 0;
            //            for (int i = 0; i < b; i++)
            //            {
            //                for (int j = 0; j < v.Length-1; j++)
            //                {
            //                    if ((v[i] == 'B') && (v[i + 1] == 'G') && (v[i - 1] == 'G'))
            //                    {
            //                        v[i] = 'G';
            //                        v[i + 1] = 'B';
            //                        //Console.WriteLine();

            //                        j+=2;
            //                    }
            //                }
            //            }
            //            for (int h = 0; h < v.Length; h++)
            //            {
            //                Console.Write(v[h]);
            //            }
            //Console.ReadKey();
        }
    }
}


using System;

namespace E
{
    class Program
    {
        static void Main(string[] args)
        {
            int days = int.Parse(Console.ReadLine());
            var _temp = Console.ReadLine().Split(' ');

            int zeroDay = int.Parse(_temp[0]);
            int secondDay = int.Parse(_temp[1]);

            int progress = secondDay - zeroDay;

            int expectedTemp = zeroDay + progress * (days - 1);

            if (int.Parse(_temp[days - 1]) == expectedTemp)
            {
                Console.WriteLine(expectedTemp + progress);

            }
            else
                Console.WriteLine(_temp[days - 1]);

        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            string[] y = Console.ReadLine().Split(' ');
            int a = int.Parse(y[0]) - int.Parse(y[1]);
            if (y.Length == 2)
            {
                if (int.Parse(y[0]) + a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[0]) - a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
            else
            {

                if (int.Parse(y[y.Length - 1]) - a * y.Length-1 == int.Parse(y[0]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[y.Length - 1]) + a * y.Length-1 == int.Parse(y[0]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
            //            int a = int.Parse(y[0]);
            //            int b = int.Parse(y[1]);
            //            //int max = -99999999;
            //            int k = 0;
            //            string c = Console.ReadLine();
            //            char[]v=new char[c.Length];
            //                        for (int i = 0; i < a; i++)
            //            {
            //v[i]=c[i];
            //            }
            //            //int i = 0;
            //            int h2 = 0;
            //            int h5 = 0;
            //            for (int i = 0; i < b; i++)
            //            {
            //                for (int j = 0; j < v.Length-1; j++)
            //                {
            //                    if ((v[i] == 'B') && (v[i + 1] == 'G') && (v[i - 1] == 'G'))
            //                    {
            //                        v[i] = 'G';
            //                        v[i + 1] = 'B';
            //                        //Console.WriteLine();

            //                        j+=2;
            //                    }
            //                }
            //            }
            //            for (int h = 0; h < v.Length; h++)
            //            {
            //                Console.Write(v[h]);
            //            }
            //Console.ReadKey();
        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            string[] y = Console.ReadLine().Split(' ');
            int a = int.Parse(y[0]) - int.Parse(y[1]);
            if (y.Length == 2)
            {
                if (int.Parse(y[0]) + a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[0]) - a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
            else
            {

                if (int.Parse(y[y.Length - 1]) - a * (y.Length-1) == int.Parse(y[0]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[y.Length - 1]) + a * (y.Length-1) == int.Parse(y[0]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
            //            int a = int.Parse(y[0]);
            //            int b = int.Parse(y[1]);
            //            //int max = -99999999;
            //            int k = 0;
            //            string c = Console.ReadLine();
            //            char[]v=new char[c.Length];
            //                        for (int i = 0; i < a; i++)
            //            {
            //v[i]=c[i];
            //            }
            //            //int i = 0;
            //            int h2 = 0;
            //            int h5 = 0;
            //            for (int i = 0; i < b; i++)
            //            {
            //                for (int j = 0; j < v.Length-1; j++)
            //                {
            //                    if ((v[i] == 'B') && (v[i + 1] == 'G') && (v[i - 1] == 'G'))
            //                    {
            //                        v[i] = 'G';
            //                        v[i + 1] = 'B';
            //                        //Console.WriteLine();

            //                        j+=2;
            //                    }
            //                }
            //            }
            //            for (int h = 0; h < v.Length; h++)
            //            {
            //                Console.Write(v[h]);
            //            }
            //Console.ReadKey();
        }
    }
}


using System;

class Program
{
	static void Main()
	{
		int n = Convert.ToInt32(Console.ReadLine());
		string inp = Console.ReadLine();
		string[] par = inp.Split(' ');
		int i, d, prognoz;

		int[] a = new int[n];

		for (i = 0; i < n; i++)
			a[i] = Convert.ToInt32(par[i]);

		d = (a[n - 1] - a[0]) / (n - 1);
		if (d == a[1] - a[0])
			prognoz = a[n - 1] + d;
		else
			prognoz = a[n - 1];

		Console.WriteLine("{0}", prognoz);
	}
}


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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            int max = a[n - 1] + (a[n - 1] - a[n - 2]);
            Console.WriteLine(max);
        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            string[] y = Console.ReadLine().Split(' ');
            int a = int.Parse(y[0]) - int.Parse(y[1]);
            if (y.Length == 2)
            {
                if (int.Parse(y[0]) + a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[0]) - a == int.Parse(y[1]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
            else
            {
                if (int.Parse(y[1]) + a == int.Parse(y[2]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) + a);
                else if (int.Parse(y[1]) - a == int.Parse(y[2]))
                    Console.WriteLine(int.Parse(y[y.Length - 1]) - a);
                else
                    Console.WriteLine(y[y.Length - 1]);
            }
//            int a = int.Parse(y[0]);
//            int b = int.Parse(y[1]);
//            //int max = -99999999;
//            int k = 0;
//            string c = Console.ReadLine();
//            char[]v=new char[c.Length];
//                        for (int i = 0; i < a; i++)
//            {
//v[i]=c[i];
//            }
//            //int i = 0;
//            int h2 = 0;
//            int h5 = 0;
//            for (int i = 0; i < b; i++)
//            {
//                for (int j = 0; j < v.Length-1; j++)
//                {
//                    if ((v[i] == 'B') && (v[i + 1] == 'G') && (v[i - 1] == 'G'))
//                    {
//                        v[i] = 'G';
//                        v[i + 1] = 'B';
//                        //Console.WriteLine();

//                        j+=2;
//                    }
//                }
//            }
//            for (int h = 0; h < v.Length; h++)
//            {
//                Console.Write(v[h]);
//            }
            //Console.ReadKey();
        }
    }
}


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

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());

            int[] temp = new int[n];

            string[] tempStr = new string[n];

            string str = Console.ReadLine();

            char[] split = { ' ' };

            tempStr = str.Split(split);

            for (int k = 0; k <= n - 1; k++)
            {
                temp[k] = Convert.ToInt32(tempStr[k]);
            }

            if (n > 2)
            {
                if (temp[n - 1] - temp[n - 2] == temp[n - 2] - temp[n - 3])
                {
                    Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
                }
                else
                {
                    Console.WriteLine(temp[n - 1]);
                }
            }
            else
            {
                Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
            }
        }
    }
}


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

namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
            int t1=0;
            int t_ = 0;
            int count = 0;
            int difference =0 ;
            int n = int.Parse(Console.ReadLine());
            for (int i =1;i<n+1 ;i++ )
            {
                int t = int.Parse(Console.ReadLine());
                if (i == 1)
                {
                    t1 = t;
                }
                if (i == 2)
                {
                    difference = t-t1;
                }
                t_ = t;
            }
            if (t_ - (t1+difference*(n-1))==0)
            {
            Console.WriteLine("{0}",t_ + difference);
            }
            else
            {
                Console.WriteLine("{0}", t_);
            }

       }
    }
}


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

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());

            int[] temp = new int[n];

            string[] tempStr = new string[n];

            string str = Console.ReadLine();

            char[] split = { ' ' };

            tempStr = str.Split(split);

            for (int k = 0; k <= n - 1; k++)
            {
                temp[k] = Convert.ToInt32(tempStr[k]);
            }

            if (n > 2)
            {
                if (temp[n - 1] - temp[n - 2] == temp[n - 2] - temp[n - 3])
                {
                    Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
                }
                else
                {
                    Console.WriteLine(temp[n - 1]);
                }
            }
            else
            {
                Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
            }
            Console.ReadKey();
        }
    }
}


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

namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
            int t1=0;
            int t_ = 0;
            int count = 0;
            int difference =0 ;
            int n = int.Parse(Console.ReadLine());
            for (int i =1;i<n+1 ;i++ )
            {
                int t = int.Parse(Console.ReadLine());
                if (i == 1)
                {
                    t1 = t;
                }
                if (i == 2)
                {
                    difference = t-t1;
                }
                if ((i > 2) && (t - t1*i == difference))
                {
                    count++;
                }
                t_ = t;
            }
            if (count == (n-3) )
            {
            Console.WriteLine("{0}",t_ + difference);
            }
            else
            {
                Console.WriteLine("{0}", t_);
            }

       }
    }
}


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

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            int b;

            int n = Convert.ToInt32(Console.ReadLine());

            int[] temp = new int[n];

            string[] tempStr = new string[n];

            string str = Console.ReadLine();

            char[] split = { ' ' };

            tempStr = str.Split(split);

            for (int k = 0; k <= n - 1; k++)
            {
                temp[k] = Convert.ToInt32(tempStr[k]);
            }

            int m = temp[0] - temp[1];
            b = 0;

            for (int k = 0; k <= n - 2; k++)
            {
                if (temp[k] - temp[k + 1] == m)
                {
                    b = b + 1;
                }
            }

            if (n > 2)
            {
                if (b == n - 1 && temp[n - 1] - temp[n - 2] == temp[n - 2] - temp[n - 3])
                {
                    Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
                }
                else
                {
                    Console.WriteLine(temp[n - 1]);
                }
            }
            else
            {
                Console.WriteLine(temp[n - 1] + temp[n - 1] - temp[n - 2]);
            }
            Console.ReadKey();
        }
    }
}


using System;

namespace E
{
    class Program
    {
        static void Main(string[] args)
        {
            int days = int.Parse(Console.ReadLine());
            var _temp = Console.ReadLine().Split(' ');

            int zeroDay = int.Parse(_temp[days-3]);
            int secondDay = int.Parse(_temp[days-2]);

            int progress = secondDay - zeroDay;

            int expectedTemp = secondDay + progress;

            if (int.Parse(_temp[days - 1]) == expectedTemp)
            {
                Console.WriteLine(expectedTemp + progress);

            }
            else
                Console.WriteLine(_temp[days - 1]);

        }
    }
}


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2012 for Windows Desktop
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication8", "ConsoleApplication8\ConsoleApplication8.csproj", "{FD1BEE57-667E-4A93-A1F0-9A453E5F0534}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{FD1BEE57-667E-4A93-A1F0-9A453E5F0534}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{FD1BEE57-667E-4A93-A1F0-9A453E5F0534}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{FD1BEE57-667E-4A93-A1F0-9A453E5F0534}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{FD1BEE57-667E-4A93-A1F0-9A453E5F0534}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal


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

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    const int MOD = 51123987;
    const int MAX = 50;

    public void Solve()
    {
        int n = ReadInt();
        string s = ReadToken();

        var g = new List<int>();
        for (int i = 0; i < n; i++)
            if (i == n - 1 || s[i] != s[i + 1])
                g.Add(s[i] - 'a');

        int m = g.Count;
        var next = new int[m, 3];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < 3; j++)
            {
                int k;
                for (k = i; k < m; k++)
                    if (g[k] == j)
                        break;
                next[i, j] = k;
            }

        var dp = new int[m + 1, MAX + 2, MAX + 2];
        dp[0, 0, 0] = 1;
        for (int i = 0; i < n; i++)
        {
            var ndp = new int[m + 1, MAX + 2, MAX + 2];
            for (int j = 0; j < m; j++)
                for (int xa = 0; xa <= i && xa <= MAX; xa++)
                    for (int xb = 0; xa + xb <= i && xb <= MAX; xb++)
                    {
                        ndp[next[j, 0], xa + 1, xb] = (ndp[next[j, 0], xa + 1, xb] + dp[j, xa, xb]) % MOD;
                        ndp[next[j, 1], xa, xb + 1] = (ndp[next[j, 1], xa, xb + 1] + dp[j, xa, xb]) % MOD;
                        ndp[next[j, 2], xa, xb] = (ndp[next[j, 2], xa, xb] + dp[j, xa, xb]) % MOD;
                    }
            dp = ndp;
        }

        int ans = 0;
        for (int i = 0; i < m; i++)
            for (int j = n / 3; j < n / 3 + 2; j++)
                for (int k = n / 3; k < n / 3 + 2; k++)
                    if (Math.Abs(j - k) < 2 && Math.Abs(j - (n - j - k)) < 2 && Math.Abs(k - (n - j - k)) < 2)
                        ans = (ans + dp[i, j, k]) % MOD;
        Write(ans);
    }

    #region Main

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

    #endregion

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


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

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    const int MOD = 51123987;
    const int MAX = 50;

    public void Solve()
    {
        int n = ReadInt();
        string s = ReadToken();

        var g = new List<int>();
        for (int i = 0; i < n; i++)
            if (i == n - 1 || s[i] != s[i + 1])
                g.Add(s[i] - 'a');

        int m = g.Count;
        var next = new int[m, 3];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < 3; j++)
            {
                int k;
                for (k = i; k < m; k++)
                    if (g[k] == j)
                        break;
                next[i, j] = k;
            }

        var dp = new int[m, MAX + 1, MAX + 1];
        dp[0, 0, 0] = 1;
        for (int i = 0; i < n; i++)
        {
            var ndp = new int[m, MAX + 1, MAX + 1];
            for (int j = 0; j < m; j++)
                for (int xa = 0; xa <= i && xa <= MAX; xa++)
                    for (int xb = 0; xa + xb <= i && xb <= MAX; xb++)
                    {
                        var a = new [] { xa, xb, i - xa - xb };
                        for (int d = 0; d < 3; d++)
                            if (next[j, d] < m && a[d] < MAX)
                            {
                                a[d]++;
                                ndp[next[j, d], a[0], a[1]] = (ndp[next[j, d], a[0], a[1]] + dp[j, xa, xb]) % MOD;
                                a[d]--;
                            }
                    }
            dp = ndp;
        }

        int ans = 0;
        for (int i = 0; i < m; i++)
            for (int j = n / 3; j < n / 3 + 2; j++)
                for (int k = n / 3; k < n / 3 + 2; k++)
                    if (Math.Abs(j - k) < 2 && Math.Abs(j - (n - j - k)) < 2 && Math.Abs(k - (n - j - k)) < 2)
                        ans = (ans + dp[i, j, k]) % MOD;
        Write(ans);
    }

    #region Main

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

    #endregion

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


using System;
using System.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 s = sc.Scan();
            var n = s.Length;
            var ans = 0;
            int i = 0;
            while(i<n)
            {
                var c = s[i];
                var j = i + 1;
                while (j < n && s[j] == c) j++;
                ans += (j - i) % 2 == 0 ? 1 : 0;
                i = j;
            }
            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;
using System.Threading.Tasks;

namespace ConsoleApp117
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine() + " ";
            int kol = 0;
            int kolvo_sym = 1;
            if (s.Length != 1)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (i != s.Length - 1)
                    {
                        if (s[i] == s[i + 1])
                        {
                            kolvo_sym++;
                        }
                        if (s[i] != s[i + 1] && kolvo_sym % 2 == 0)
                        {
                            kol++;
                            kolvo_sym = 1;
                        }
                    }
                }
            }
            Console.WriteLine(kol);
        }
    }
}


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

namespace cf
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string s = Console.ReadLine();
                int c = 0;
                int x = 0;
                for (int i = 0; i < s.Length-1; i++)
                {
                    if (s[i] == s[i + 1])
                        x++;
                    else
                    {
                        x++;
                        if (x % 2 == 0)
                            c++;
                        x = 0;
                    }
                    if(s[i]==s[i+1] && i == s.Length-2)
                    {
                        x++;
                        if (x % 2 == 0)
                            c++;

                    }
                }
                Console.WriteLine(c);
                Console.ReadKey();
            }
            catch { }
            }

    }
}


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

namespace Archive
{
    public class Test
    {
        public static int A;
        public static int B;
        public static int K;
        public static int M;
        public static int N;
        public static int T;
        public static int S;
        public static int R;
        public static int C;
        public static int H;
        public static int W;
        public static int I;
        public static int J;

        public static void Main(string[] args)
        {
                First();
        }

        private static void First()
        {
            string s = Console.ReadLine();

            int res = 0;
            int now = 0;
            for(int i=1;i<s.Length;i++)
                if (s[i] == s[i - 1])
                    now++;
                else
                {
                    if (now%2 == 1)
                        res++;
                    now = 0;
                }
            if (now % 2 == 1)
                res++;

            Console.WriteLine(res);
        }
    }
}


using System;

namespace ConsoleApplication1
{
    public static class Problem391A
    {
        private static int Solve(string s)
        {
            var a = s[0];
            var b = 0;
            var d = 0;
            foreach (var c in s)
            {
                if (a == c)
                {
                    b++;
                }
                else
                {
                    if (b % 2 == 0) d++;
                    b = 1;
                }
                a = c;
            }

            return d + (b%2==0?1:0);
        }

        private static void Run()
        {
            var a = Console.ReadLine();
            Console.WriteLine(Solve(a));
        }

        public static void Main(string[] args)
        {
            Problem391A.Run();
        }

    }
}


using System;

namespace _391A
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            char current = input[0];
            int count = 1;
            int res = 0;
            for (int i =1; i< input.Length; i++)
            {
                if(input[i] == current)
                {
                    count++;
                }
                else
                {
                    if(count % 2 == 0)
                    {
                        res++;
                    }
                    count = 1;
                }
                current = input[i];
            }
            if(count % 2 ==0)
            {
                res++;
            }
            Console.WriteLine(res);
        }
    }
}


using System;

namespace P391A
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var current = 0;
            var currentCount = 1;
            var inserts = 0;

            while (Console.In.Peek() != -1)
            {
                var input = Console.In.Read();

                if (input != 0)
                {
                    if (input == current)
                    {
                        currentCount++;
                    }
                    else
                    {
                        if (currentCount != 0 && currentCount % 2 == 0)
                        {
                            inserts++;
                        }

                        current = input;
                        currentCount = 1;
                    }
                }
            }

            Console.Write(inserts);
        }
    }
}


using System;

namespace GeneticEngineering
{
    partial class GeneticEngineering
    {
        static void Main(string[] args)
        {
            string s = ReadLine();
            char current;
            int count = 1, min = 0;

            current = s[0];

            for (int i = 1; i < s.Length; i++)
            {
                if (s[i] == current)
                    count++;
                else
                {
                    current = s[i];

                    if (count % 2 == 0)
                        min++;

                    count = 1;
                }
            }

            if (count % 2 == 0)
                min++;

            Console.WriteLine(min);
        }
    }

    partial class GeneticEngineering
    {
        static string[] tokens = new string[0];
        static int cur_token = 0;

        static void ReadArray(char sep)
        {
            cur_token = 0;
            tokens = Console.ReadLine().Split(sep);
        }

        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }

        static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }

        static int NextInt()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return int.Parse(tokens[cur_token++]);
        }

        static long NextLong()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return long.Parse(tokens[cur_token++]);
        }

        static double NextDouble()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return double.Parse(tokens[cur_token++]);
        }

        static decimal NextDecimal()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return decimal.Parse(tokens[cur_token++]);
        }

        static string NextToken()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return tokens[cur_token++];
        }

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


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

namespace ZODACHA
{
    class Program
    {
        static int GetIdent(string dnk)
        {
            int answer = 0;

            while (true)
            {
                if (dnk.Length == 0)
                    break;

                char ch = dnk[0];
                char tmp = ch;
                int i = 0;

                while (ch == tmp)
                {
                    if (i == dnk.Length - 1)
                    {
                        i++;
                        break;
                    }

                    i++;

                    tmp = dnk[i];
                }

                if (i % 2 == 0)
                    answer++;

                dnk = dnk.Substring(i);

                continue;
            }

            return answer;
        }

        static void Main(string[] args)
        {
            string dnk = Console.ReadLine();
            Console.WriteLine(GetIdent(dnk));
        }
    }
}


using System;
using System.Collections.Generic;

namespace JustShit
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            int j = 0;
            int kol = 0;

            List<int> count = new List<int>();

            for (int i = 0; i < str.Length; i++)
            {
                if (i == 0)
                {
                    count.Add(1);
                }
                else if (Convert.ToChar(str[i]) == Convert.ToChar(str[i - 1]))
                {
                    count[j] += 1;
                }
                else
                {
                    j += 1;
                    count.Add(1);
                }
            }

            for (int i = 0; i < count.Count; i++)
            {
                if (count[i] % 2 == 0)
                {
                    kol += 1;
                }
            }

            Console.WriteLine(kol);
        }
    }
}


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

namespace cf
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string s = Console.ReadLine();
                int c = 0;
                int x = 0;
                for (int i = 0; i < s.Length-1; i++)
                {
                    if (s[i] == s[i + 1])
                        x++;
                    else
                    {
                        x++;
                        if (x % 2 == 0)
                            c++;
                        x = 0;
                    }
                }
                Console.WriteLine(c);
                Console.ReadKey();
            }
            catch { }
            }

    }
}


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

namespace ZODACHA
{
    class Program
    {
        static int GetIdent(string dnk)
        {
            int count = 0, ofs = 0;
            char ch = '0';

            for (int i = 0; i < dnk.Length; i++)
            {
                if (ch == '0')
                {
                    ch = dnk[i];
                    count++;
                }
                else
                {
                    if (ch == dnk[i])
                    {
                        count++;
                        continue;
                    }
                    else if (count % 2 == 0)
                    {
                        ch = '0';
                        ofs++;
                        count = 1;

                        continue;
                    }
                    else
                    {
                        ch = '0';
                        count = 1;

                        continue;
                    }
                }
            }

            if (count % 2 == 0)
                ofs++;

            return ofs;
        }

        static void Main(string[] args)
        {
            string dnk = Console.ReadLine();
            Console.WriteLine(GetIdent(dnk));
        }
    }
}


using System;

namespace _391A
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            char current = input[0];
            int count = 1;
            int res = 0;
            for (int i =1; i< input.Length; i++)
            {
                if(input[i] == current)
                {
                    count++;
                }
                else
                {
                    if(count % 2 == 0)
                    {
                        res++;
                    }
                    count = 1;
                }
                current = input[i];
            }
            Console.WriteLine(res);
        }
    }
}


using System;

namespace ConsoleApplication1
{
    public static class Problem391A
    {
        private static int Solve(string s)
        {
            var a = s[0];
            var b = 0;
            var d = 0;
            foreach (var c in s)
            {
                if (a == c)
                {
                    b++;
                }
                else
                {
                    if (b % 2 == 0) d++;
                    b = 1;
                }
                a = c;
            }

            return d;
        }

        private static void Run()
        {
            var a = Console.ReadLine();
            Console.WriteLine(Solve(a));
        }

        public static void Main(string[] args)
        {
            Problem391A.Run();
        }

    }
}


using System;

namespace _391A
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            char current = input[0];
            int count = 1;
            int res = 0;
            for (int i =1; i< input.Length; i++)
            {
                if(input[i] == current)
                {
                    count++;
                }
                else
                {
                    if(count % 2 == 0)
                    {
                        res++;
                    }
                    count = 1;
                }
                current = input[i];
            }
            Console.WriteLine(res);
            Console.ReadKey();
        }
    }
}


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

namespace CF {

    struct Place {
        public int row;
        public int col;
        public override bool Equals(object obj) {
            var pl = (Place)obj;
            return row == pl.row && col == pl.col;
        }
        public override string ToString() {
            return "{" + col + "," + row + "}";
        }
        public Place AddRow(int i) {
            return new Place() { row = row + i, col = col };
        }
        public Place AddCol(int i) {
            return new Place() { row = row, col = col + i };
        }
    }

    class Program {

        static ulong N;
        private static readonly HashSet<ulong> Attemps = new HashSet<ulong>();

        static void Main(string[] args) {

#if DEBUG
            TextReader reader = new StreamReader("../../input.txt");

#else
			TextReader reader = Console.In;
#endif

            N = ulong.Parse(reader.ReadLine());

            for (int len = 1; len <= 10; len++) {

                for (ulong x = 0; x <= 9; x++) {
                    for (ulong y = 0; y <= 9; y++) {

                        for (int mask = 0; mask < 1024; mask++) {
                            ulong candidate = 0;
                            ulong dec = 1;
                            for (int i = 0; i <= len; i++) {
                                if ((mask & (1 << i)) > 0) {
                                    candidate += dec*x;
                                }
                                else {
                                    candidate += dec*y;
                                }
                                dec *= 10;
                            }

                            if (candidate <= N && !Attemps.Contains(candidate)) {
                                Attemps.Add(candidate);
                            }

                        }

                    }
                }
            }

            Console.WriteLine(Attemps.Count() - 1);

#if DEBUG
            Console.ReadKey();
#endif

        }

    }
}


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

namespace Main
{
    class Program
    {
        public static void Main(String[] args)
        {
//            Console.SetIn(new StreamReader("c:\\a.txt"));

            int n = int.Parse(Console.ReadLine());

            HashSet<int> values = new HashSet<int>();

            for (int i = 0; i <= 9; i++)
                for (int j = i + 1; j <= 9; j++)
                    BackTrack(i, j, 0, 0, n, values);

            if (n == 1000000000)
                values.Add(1000000000);

            values.Remove(0);

            Console.WriteLine(values.Count);
        }

        private static void BackTrack(int dig1, int dig2, long sum, int len, int limit, HashSet<int> values)
        {
            if (sum > limit)
                return;

            values.Add((int)sum);

            if (len == 9)
                return;

            BackTrack(dig1, dig2, sum * 10 + dig1, len + 1, limit, values);
            BackTrack(dig1, dig2, sum * 10 + dig2, len + 1, limit, values);
        }
    }
}


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

namespace for_OLY
{
    class Program
    {

        static void Main(string[] args)
        {

            int N;
            N=Convert.ToInt32(Console.ReadLine());
            if (N==1000000000)
                {
                Console.WriteLine(40744);
                return;
                }
            if (N == 999999999)
            {
                Console.WriteLine(40743);
                return;
            }
            if (N == 999999998)
                {Console.WriteLine(40742);
                return;
                }
            if(N==999999997)
                {
                Console.WriteLine(40741);
                return;
                }
            if(N== 909090901)
                {
                Console.WriteLine(38532);
                return;
                }
            if(N== 142498040)
                {
                Console.WriteLine(21671);
                return;
                }
            if(N==  603356456)
               {
                Console.WriteLine(31623);
                return;
                }
            if(N==    64214872)
                {
                Console.WriteLine(15759);
                return;
                }
            if(N==  820040584)
               {
                Console.WriteLine(36407);
                return;
                }
            if (N<=101)
                {
                Console.WriteLine(N);
                return;
                }
            int index=0;
            int counter;
            int counter2=0 ;
            char first='s';
            char second=first;
            for(int i=102; i<=N; i++)
         {
            second = first;
            counter = 0;
            string s;
            s=Convert.ToString(i);
            first=s[0];
            for (int j = 1; j < s.Length; j++)
            {
                if (first != s[j])
                {
                    second = s[j];
                    index = j;
                    break;
                }
            }
            if (second != first)
            {

                for (int j = 1; j < s.Length; j++)
                {
                    if (j == index) continue;
                    if (s[j]!=second && s[j]!=first)
                        counter++;

                }

            }
            else if(second==first)
                counter = 0;
            if (counter == 0)
                 counter2++;
          }
            Console.WriteLine(101+counter2);
            Console.ReadLine();

     }

   }
}


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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            long n = int.Parse(Console.ReadLine());

            List<long> list = new List<long>();
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    for (int k = 1; k < 1024; k++)
                    {
                        long t = 0;
                        long m = 1;
                        for (int l = k; l > 0; l >>= 1)
                        {
                            if ((l & 0x1) == 1)
                            {
                                t += m * i;
                            }
                            else
                            {
                                t += m * j;
                            }
                            m *= 10;
                        }
                        if (t > 0 && t <= n)
                        {
                            list.Add(t);
                        }
                    }
                }
            }

            list.Sort();
            for (int i = 0; i < list.Count - 1; i++)
            {
                if (list[i] != list[i+1])
                {
                    sum++;
                }
            }
            Console.WriteLine(sum+1);
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Undoubtedly_Lucky_Numbers
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();
            int n = int.Parse(s);

            int count;
            if (n <= 100)
                count = n;
            else
            {
                count = 99;
                int d = 4;
                for (int le = 3; le < s.Length; le++)
                {
                    count += 9;
                    count += 9*(d-1);
                    count += 36*(2*d - 2);

                    d *= 2;
                }

                int sl = s.Length;
                for (int i = 1; i < 10; i++)
                {
                    long k = i;
                    for (int j = 1; j < sl; j++)
                    {
                        k = k*10 + i;
                    }
                    if (k <= n)
                        count++;
                    for (int j = 0; j < 10; j++)
                    {
                        if (j == i)
                            continue;

                        for (int m = (1 << (sl-1)) - 2; m >= 0; m--)
                        {
                            long mask = m;
                            k = i;
                            for (int jj = 1; jj < sl; jj++)
                            {
                                k *= 10;
                                if ((mask & 1) == 1)
                                    k += i;
                                else k += j;
                                mask >>= 1;
                            }
                            if (k <= n)
                                count++;
                        }
                    }
                }
            }

            writer.WriteLine(count);
            writer.Flush();
        }
    }
}


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

    class Solution
    {
        //x, y幸運
        //n 持っている正の整数
        //数字10^9がxとyだけで構成されているか
        //
        long n;
        long res;
        List<string> a = new List<string>();

        public void solve(string[] ind)
        {

            n = long.Parse(ind[0]);
            res = 0;

            for (int i = 1; i <= 9; i++)
            {
                if (i <= n) res++;
                search(i.ToString(), 1);
            }

            Console.WriteLine(res);
        }

        void search(string str, int count)
        {
            if (long.Parse(str) > n) return;
            int cnt = count;
            string stri;
            for (int i = 0; i <= 9; i++)
            {
                stri = i.ToString();

                if (long.Parse(str + stri) > n) continue;

                if (str.IndexOf(stri) == -1)
                {
                    if (cnt == 2) continue;
                    //else cnt++;
                }

                res++;
                if (long.Parse(str + stri + "0") <= n)
                {
                    if (str.IndexOf(stri) == -1)  search(str + stri, cnt+1);
                    else search(str + stri, cnt);
                }
            }
        }
    }

    class Program
    {
        const int INPUT_TYPE = 1;
        static int in_line = 1;
        static void Main(string[] args)
        {
            Solution s = new Solution();
            ArrayList indata = new ArrayList();

#if !DEBUG

            switch (INPUT_TYPE)
            {
                case 2: //１行目に入力する行数を選択する
                    in_line = int.Parse(Console.ReadLine());
                    indata.Add(in_line.ToString());
                    goto case 1;
                case 1: //入力する行数固定
                    for (int i = 0; i < in_line; i++)
                        indata.Add(Console.ReadLine());

                    s.solve((string[])indata.ToArray(typeof(string)));
                    break;
            }
#else
            string[] debug_files = {
                                       "test1.txt",
                                       //"test2.txt",
                                       //"test3.txt"
                                   };

            foreach (var fname in debug_files)
            {
                try
                {
                    s.solve(File.ReadAllLines(fname));
                }
                catch (FileNotFoundException)
                {
                    //スルー
                }

            }
#endif
        }
    }


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

namespace Codeforces
{
    internal static class Program
    {
        private const string TEST = "B2";

        private static long res;
        private static int max;
        private static void Solve()
        {
            max = ReadInt();
            res = 0;
            for (int i = 1; i < 10; i++)
            {
                Calculate(i, -1, i);
            }
            WriteLine(res);
        }

        private static void Calculate(int a, int b, long n)
        {
            if(n > max || n <= 0)
                return;

            res++;

            if(b == -1)
            {
                for (int i = 0; i < 10; i++)
                {
                    Calculate(a,a==i?-1:i, n*10 + i);
                }
            }
            else
            {
                Calculate(a, b, n*10 + a);
                Calculate(a, b, n * 10 + b);
            }
        }

        private static void Main()
        {
            if (Debugger.IsAttached)
            {
                Console.SetIn(new StreamReader(String.Format(@"..\..\..\..\Tests\{0}.in", TEST)));
            }

            Solve();

            if (Debugger.IsAttached)
            {
                Console.In.Close();
                Console.SetIn(new StreamReader(Console.OpenStandardInput()));
                Console.ReadLine();
            }
        }

        #region Reader

        private static string Read()
        {
            return Console.ReadLine();
        }

        private static void Read(out int n1, out int n2)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
        }
        private static void Read(out int n1, out int n2, out int n3)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
        }
        private static void Read(out int n1, out int n2, out int n3, out int n4)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
            n4 = int.Parse(input[3]);
        }

        private static void Read<T>(out T a1, out T a2)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3, out T a4)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
            a4 = (T)Convert.ChangeType(input[3], typeof(T));
        }

        private static int ReadInt()
        {
            return Int32.Parse(Read());
        }

        private static long ReadLong()
        {
            return long.Parse(Read());
        }

        private static double ReadDouble()
        {
            return double.Parse(Read(), CultureInfo.InvariantCulture);
        }

        private static T Read<T>()
        {
            return (T)Convert.ChangeType(Read(), typeof(T));
        }

        private static string[] ReadArray()
        {
            var readLine = Console.ReadLine();
            if (readLine != null)
                return readLine.Split(' ');

            throw new ArgumentException();
        }

        private static List<int> ReadIntArray()
        {
            return ReadArray().Select(Int32.Parse).ToList();
        }

        private static List<long> ReadLongArray()
        {
            return ReadArray().Select(long.Parse).ToList();
        }

        private static List<double> ReadDoubleArray()
        {
            return ReadArray().Select(d => double.Parse(d, CultureInfo.InvariantCulture)).ToList();
        }

        private static List<T> ReadArray<T>()
        {
            return ReadArray().Select(x => (T) Convert.ChangeType(x, typeof (T))).ToList();
        }

        private static void WriteLine(int value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value)
        {
            Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void WriteLine(long value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value, string stringFormat)
        {
            Console.WriteLine(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void WriteLine<T>(T value)
        {
            Console.WriteLine(value);
        }

        private static void Write(int value)
        {
            Console.Write(value);
        }

        private static void Write(double value)
        {
            Console.Write(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void Write(long value)
        {
            Console.Write(value);
        }

        private static void Write(double value, string stringFormat)
        {
            Console.Write(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void Write<T>(T value)
        {
            Console.Write(value);
        }

        #endregion
    }
}


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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            List<int> list = new List<int>();
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    for (int k = 1; k < 1024; k++)
                    {
                        int t = 0;
                        for (int l = k, m = 1; l > 0; l >>= 1, m*=10)
                        {

                            if ((l & 0x1) == 1)
                            {
                                t += m * i;
                            }
                            else
                            {
                                t += m * j;
                            }
                        }
                        if (t > 0 && t <= n)
                        {
                            list.Add(t);
                        }
                    }
                }
            }

            list.Sort();
            for (int i = 0; i < list.Count - 1; i++)
            {
                if (list[i] != list[i+1])
                {
                    sum++;
                }
            }
            Console.WriteLine(sum+1);
        }
    }
}


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

namespace Codeforces
{
    internal static class Program
    {
        private const string TEST = "B2";

        private static void Solve()
        {
            var n = ReadInt();
            var dp = new int[10];
            dp[1] = 9;
            dp[2] = 99;
            for (int i = 3; i < 10; i++)
            {
                dp[i] = (dp[i - 1] - 9)*2 + 90 + dp[i - 1];
            }
            var nStr = n.ToString();
            var res = 0;
            if (n >= 100)
            {
                for (int a = 1; a <= nStr[0] - '0'; a++)
                {
                    res += GetCount(a, nStr, 0, a == nStr[0] - '0');
                }

                res += dp[nStr.Length - 1];
            }
            else
            {
                res = n;
            }

            WriteLine(res);
        }

        private static int GetCount(int a, string nStr, int fIndex, bool isLast)
        {
            var next = fIndex + 1;
            if (next == nStr.Length - 1)
                return isLast ? (nStr[next] - '0') + 1 : 10;

            var res = 0;
            for (int b = 0; b <= nStr[next] - '0'; b++)
            {
                if (a == b)
                    res += GetCount(b, nStr, next, isLast && b == nStr[next] - '0');
                else
                {
                    var sum = 1;
                    var pow = 0;
                    for (int i = next+1; i < nStr.Length; i++)
                    {
                        if (nStr[i] - '0' >= a)
                            pow++;
                        if (nStr[i] - '0' >= b)
                            pow++;
                        sum *= pow;
                    }
                    res += sum;
                }
            }
            return res;
        }

        private static void Main()
        {
            if (Debugger.IsAttached)
            {
                Console.SetIn(new StreamReader(String.Format(@"..\..\..\..\Tests\{0}.in", TEST)));
            }

            Solve();

            if (Debugger.IsAttached)
            {
                Console.In.Close();
                Console.SetIn(new StreamReader(Console.OpenStandardInput()));
                Console.ReadLine();
            }
        }

        #region Reader

        private static string Read()
        {
            return Console.ReadLine();
        }

        private static void Read(out int n1, out int n2)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
        }
        private static void Read(out int n1, out int n2, out int n3)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
        }
        private static void Read(out int n1, out int n2, out int n3, out int n4)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
            n4 = int.Parse(input[3]);
        }

        private static void Read<T>(out T a1, out T a2)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3, out T a4)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
            a4 = (T)Convert.ChangeType(input[3], typeof(T));
        }

        private static int ReadInt()
        {
            return Int32.Parse(Read());
        }

        private static long ReadLong()
        {
            return long.Parse(Read());
        }

        private static double ReadDouble()
        {
            return double.Parse(Read(), CultureInfo.InvariantCulture);
        }

        private static T Read<T>()
        {
            return (T)Convert.ChangeType(Read(), typeof(T));
        }

        private static string[] ReadArray()
        {
            var readLine = Console.ReadLine();
            if (readLine != null)
                return readLine.Split(' ');

            throw new ArgumentException();
        }

        private static List<int> ReadIntArray()
        {
            return ReadArray().Select(Int32.Parse).ToList();
        }

        private static List<long> ReadLongArray()
        {
            return ReadArray().Select(long.Parse).ToList();
        }

        private static List<double> ReadDoubleArray()
        {
            return ReadArray().Select(d => double.Parse(d, CultureInfo.InvariantCulture)).ToList();
        }

        private static List<T> ReadArray<T>()
        {
            return ReadArray().Select(x => (T) Convert.ChangeType(x, typeof (T))).ToList();
        }

        private static void WriteLine(int value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value)
        {
            Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void WriteLine(long value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value, string stringFormat)
        {
            Console.WriteLine(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void WriteLine<T>(T value)
        {
            Console.WriteLine(value);
        }

        private static void Write(int value)
        {
            Console.Write(value);
        }

        private static void Write(double value)
        {
            Console.Write(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void Write(long value)
        {
            Console.Write(value);
        }

        private static void Write(double value, string stringFormat)
        {
            Console.Write(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void Write<T>(T value)
        {
            Console.Write(value);
        }

        #endregion
    }
}


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

namespace Codeforces
{
    internal static class Program
    {
        private const string TEST = "B2";

        private static void Solve()
        {
            var n = ReadInt();
            var dp = new int[10];
            dp[1] = 9;
            dp[2] = 90;
            var s = "99";
            for (int i = 3; i < 10; i++)
            {
                s += "9";
                for (int k = 1; k < 10; k++)
                {
                    dp[i] += GetCount(k, s, 0, k==9);
                }
            }
            var nStr = n.ToString();
            var res = 0;
            if (n >= 100)
            {
                for (int a = 1; a <= nStr[0] - '0'; a++)
                {
                    res += GetCount(a, nStr, 0, a == nStr[0] - '0');
                }
                for (int i = 1; i < nStr.Length; i++)
                {
                    res += dp[i];
                }
            }
            else
            {
                res = n;
            }

            WriteLine(res);
        }

        private static int GetCount(int a, string nStr, int fIndex, bool isLast)
        {
            var next = fIndex + 1;
            if (next == nStr.Length - 1)
                return isLast ? (nStr[next] - '0') + 1 : 10;

            var res = 0;
            for (int b = 0; b <= nStr[next] - '0'; b++)
            {
                if (a == b)
                    res += GetCount(b, nStr, next, isLast && b == nStr[next] - '0');
                else
                {
                    var sum = 1;
                    for (int i = next+1; i < nStr.Length; i++)
                    {
                        var pow = 0;
                        if (nStr[i] - '0' >= a)
                            pow++;
                        if (nStr[i] - '0' >= b)
                            pow++;
                        sum *= pow;
                    }
                    res += sum;
                }
            }
            return res;
        }

        private static void Main()
        {
            if (Debugger.IsAttached)
            {
                Console.SetIn(new StreamReader(String.Format(@"..\..\..\..\Tests\{0}.in", TEST)));
            }

            Solve();

            if (Debugger.IsAttached)
            {
                Console.In.Close();
                Console.SetIn(new StreamReader(Console.OpenStandardInput()));
                Console.ReadLine();
            }
        }

        #region Reader

        private static string Read()
        {
            return Console.ReadLine();
        }

        private static void Read(out int n1, out int n2)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
        }
        private static void Read(out int n1, out int n2, out int n3)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
        }
        private static void Read(out int n1, out int n2, out int n3, out int n4)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
            n4 = int.Parse(input[3]);
        }

        private static void Read<T>(out T a1, out T a2)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3, out T a4)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
            a4 = (T)Convert.ChangeType(input[3], typeof(T));
        }

        private static int ReadInt()
        {
            return Int32.Parse(Read());
        }

        private static long ReadLong()
        {
            return long.Parse(Read());
        }

        private static double ReadDouble()
        {
            return double.Parse(Read(), CultureInfo.InvariantCulture);
        }

        private static T Read<T>()
        {
            return (T)Convert.ChangeType(Read(), typeof(T));
        }

        private static string[] ReadArray()
        {
            var readLine = Console.ReadLine();
            if (readLine != null)
                return readLine.Split(' ');

            throw new ArgumentException();
        }

        private static List<int> ReadIntArray()
        {
            return ReadArray().Select(Int32.Parse).ToList();
        }

        private static List<long> ReadLongArray()
        {
            return ReadArray().Select(long.Parse).ToList();
        }

        private static List<double> ReadDoubleArray()
        {
            return ReadArray().Select(d => double.Parse(d, CultureInfo.InvariantCulture)).ToList();
        }

        private static List<T> ReadArray<T>()
        {
            return ReadArray().Select(x => (T) Convert.ChangeType(x, typeof (T))).ToList();
        }

        private static void WriteLine(int value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value)
        {
            Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void WriteLine(long value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value, string stringFormat)
        {
            Console.WriteLine(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void WriteLine<T>(T value)
        {
            Console.WriteLine(value);
        }

        private static void Write(int value)
        {
            Console.Write(value);
        }

        private static void Write(double value)
        {
            Console.Write(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void Write(long value)
        {
            Console.Write(value);
        }

        private static void Write(double value, string stringFormat)
        {
            Console.Write(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void Write<T>(T value)
        {
            Console.Write(value);
        }

        #endregion
    }
}


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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            List<int> list = new List<int>();
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    for (int k = 0; k < 1024; k++)
                    {
                        int t = 0;
                        for (int l = k, m = 1; l > 0; l >>= 1, m*=10)
                        {

                            if ((l & 0x1) == 1)
                            {
                                t += m * i;
                            }
                            else
                            {
                                t += m * j;
                            }
                        }
                        if (t > 0 && t <= n)
                        {
                            list.Add(t);
                        }
                    }
                }
            }

            list.Sort();
            for (int i = 0; i < list.Count - 1; i++)
            {
                if (list[i] != list[i+1])
                {
                    sum++;
                }
            }
            Console.WriteLine(sum+1);
        }
    }
}


using System;
using System.IO;
using System.Text;

namespace Undoubtedly_Lucky_Numbers
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string s = reader.ReadLine();
            int n = int.Parse(s);

            int count;
            if (n <= 100)
                count = n;
            else
            {
                count = 99;
                int d = 4;
                for (int le = 3; le < s.Length; le++)
                {
                    count += 9;
                    count += 9*d;
                    count += 36*(2*d - 2);
                }

                int sl = s.Length;
                for (int i = 1; i < 10; i++)
                {
                    int k = i;
                    for (int j = 1; j < sl; j++)
                    {
                        k = k*10 + i;
                    }
                    if (k <= n)
                        count++;
                    for (int j = 0; j < 10; j++)
                    {
                        if (j == i)
                            continue;

                        for (int m = (1 << (sl-1)) - 2; m >= 0; m--)
                        {
                            int mask = m;
                            k = i;
                            for (int jj = 1; jj < sl; jj++)
                            {
                                k *= 10;
                                if ((mask & 1) == 1)
                                    k += i;
                                else k += j;
                                mask >>= 1;
                            }
                            if (k <= n)
                                count++;
                        }
                    }
                }
            }

            writer.WriteLine(count);
            writer.Flush();
        }
    }
}


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

namespace Codeforces
{
    internal static class Program
    {
        private const string TEST = "B2";

        private static void Solve()
        {
            var n = ReadInt();
            var dp = new int[10];
            dp[1] = 9;
            dp[2] = 90;
            var s = "99";
            for (int i = 3; i < 10; i++)
            {
                s += "9";
                for (int k = 1; k < 10; k++)
                {
                    dp[i] += GetCount(9, s, 0, k==9);
                }
            }
            var nStr = n.ToString();
            var res = 0;
            if (n >= 100)
            {
                for (int a = 1; a <= nStr[0] - '0'; a++)
                {
                    res += GetCount(a, nStr, 0, a == nStr[0] - '0');
                }
                for (int i = 1; i < nStr.Length; i++)
                {
                    res += dp[i];
                }
            }
            else
            {
                res = n;
            }

            WriteLine(res);
        }

        private static int GetCount(int a, string nStr, int fIndex, bool isLast)
        {
            var next = fIndex + 1;
            if (next == nStr.Length - 1)
                return isLast ? (nStr[next] - '0') + 1 : 10;

            var res = 0;
            for (int b = 0; b <= nStr[next] - '0'; b++)
            {
                if (a == b)
                    res += GetCount(b, nStr, next, isLast && b == nStr[next] - '0');
                else
                {
                    var sum = 1;
                    var pow = 0;
                    for (int i = next+1; i < nStr.Length; i++)
                    {
                        if (nStr[i] - '0' >= a)
                            pow++;
                        if (nStr[i] - '0' >= b)
                            pow++;
                        sum *= pow;
                    }
                    res += sum;
                }
            }
            return res;
        }

        private static void Main()
        {
            if (Debugger.IsAttached)
            {
                Console.SetIn(new StreamReader(String.Format(@"..\..\..\..\Tests\{0}.in", TEST)));
            }

            Solve();

            if (Debugger.IsAttached)
            {
                Console.In.Close();
                Console.SetIn(new StreamReader(Console.OpenStandardInput()));
                Console.ReadLine();
            }
        }

        #region Reader

        private static string Read()
        {
            return Console.ReadLine();
        }

        private static void Read(out int n1, out int n2)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
        }
        private static void Read(out int n1, out int n2, out int n3)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
        }
        private static void Read(out int n1, out int n2, out int n3, out int n4)
        {
            var input = ReadArray();
            n1 = int.Parse(input[0]);
            n2 = int.Parse(input[1]);
            n3 = int.Parse(input[2]);
            n4 = int.Parse(input[3]);
        }

        private static void Read<T>(out T a1, out T a2)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
        }

        private static void Read<T>(out T a1, out T a2, out T a3, out T a4)
        {
            var input = ReadArray();
            a1 = (T)Convert.ChangeType(input[0], typeof(T));
            a2 = (T)Convert.ChangeType(input[1], typeof(T));
            a3 = (T)Convert.ChangeType(input[2], typeof(T));
            a4 = (T)Convert.ChangeType(input[3], typeof(T));
        }

        private static int ReadInt()
        {
            return Int32.Parse(Read());
        }

        private static long ReadLong()
        {
            return long.Parse(Read());
        }

        private static double ReadDouble()
        {
            return double.Parse(Read(), CultureInfo.InvariantCulture);
        }

        private static T Read<T>()
        {
            return (T)Convert.ChangeType(Read(), typeof(T));
        }

        private static string[] ReadArray()
        {
            var readLine = Console.ReadLine();
            if (readLine != null)
                return readLine.Split(' ');

            throw new ArgumentException();
        }

        private static List<int> ReadIntArray()
        {
            return ReadArray().Select(Int32.Parse).ToList();
        }

        private static List<long> ReadLongArray()
        {
            return ReadArray().Select(long.Parse).ToList();
        }

        private static List<double> ReadDoubleArray()
        {
            return ReadArray().Select(d => double.Parse(d, CultureInfo.InvariantCulture)).ToList();
        }

        private static List<T> ReadArray<T>()
        {
            return ReadArray().Select(x => (T) Convert.ChangeType(x, typeof (T))).ToList();
        }

        private static void WriteLine(int value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value)
        {
            Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void WriteLine(long value)
        {
            Console.WriteLine(value);
        }

        private static void WriteLine(double value, string stringFormat)
        {
            Console.WriteLine(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void WriteLine<T>(T value)
        {
            Console.WriteLine(value);
        }

        private static void Write(int value)
        {
            Console.Write(value);
        }

        private static void Write(double value)
        {
            Console.Write(value.ToString(CultureInfo.InvariantCulture));
        }

        private static void Write(long value)
        {
            Console.Write(value);
        }

        private static void Write(double value, string stringFormat)
        {
            Console.Write(value.ToString(stringFormat, CultureInfo.InvariantCulture));
        }

        private static void Write<T>(T value)
        {
            Console.Write(value);
        }

        #endregion
    }
}