#include <bits/stdc++.h>

#define max(a,b) a>=b?a:b
#define min(a,b) a<=b?a:b

using namespace std;

long int n , m;

int main()
{
    long int i , j , a , b , c , d , e;
    scanf("%ld %ld" , &n , &m);
    a = min(n,m);
    b = max(n,m);
    if(b == a) {
        e = 0;
        for(i = 0; i < a; i++) {
            c = (i-a)*(i-a) + (i+1)*(i+1);
            d = sqrt(c);
            if(d*d == c) {
                e = 1;
            }
        }
        if(e == 0) {
            printf("%ld\n" , a+1);
            for(i = 0; i < a; i++) {
                printf("%ld %ld\n" , i , i+1);
            }
            printf("%ld %ld\n" , a , e);
        } else {
            printf("%ld\n" , a);
            for(i = 0; i < a; i++) {
                printf("%ld %ld\n" , i , i+1);
            }
        }

    } else if(b != a) {
        a++;
        printf("%ld\n" , a);
        for(i = 0; i < a; i++) {
            printf("%ld %ld\n" , i , i+1);
        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,ca=0,cb=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
				h--;

			 ind=i+1;
			 g++;
			 ca++;
	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
				g--;

               cb++;
			   in=i+1;
		       h++;
		}
		if(g>0&&h>0)
		{
			cout<<"YES";
			return 0;
		}

   }
if((ca>=2)&&(cb>=2))
cout<<"YES";
else
   cout<<"NO";

     return 0;
 }


#include<bits/stdc++.h>
using namespace std;
int main()
 {   string s,r,hhh;
	 size_t hh;
	 cin>>s;
	 size_t t=s.size();
	 if(t%2==0)
	  {
		  hh=t/2;
			 r=s.substr(0,hh);
			 //cout<<r<<" ";
	         hhh=s.substr(hh,t);
	         //cout<<hhh;
	         sort(r.begin(),r.end());
	         sort(hhh.begin(),hhh.end());
		   if (r.compare(hhh) != 0)
                   cout<<"YES";
                   else
                    cout<<"NO";
		//  cout<<"YES";

	  }
	  else
	    {
			hh=t/2;
			 r=s.substr(0,hh);
	         hhh=s.substr(hh+1,t);
	         sort(r.begin(),r.end());
	         sort(hhh.begin(),hhh.end());
	         if (r.compare(hhh) != 0)
                   cout<<"YES";
                   else
                    cout<<"NO";

  }

	 return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,ca=0,cb=0,la=0,lb=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
			{
					h--;
					la++;
			}
		   else
		      ca++;
			 ind=i+1;
			 g++;

	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
			  {
				  g--;
				  lb++;
			  }
            else
               cb++;
			   in=i+1;
		       h++;

		}
		if((g>0&&h>0)||(ca>0&&cb>0)||(la>=2&&lb>=1)||(lb>=2&&la>=1))
		{
			cout<<"YES";
			return 0;
		}
		else if(((ca>0)&&(lb!=0))||((cb>0)&&(la!=0)))
		{
			cout<<"YES";
			return 0;
		}

   }

   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,f=0,f1=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in&&f==0)
			{
					f=1;
					h--;

			}

			 ind=i+1;
			 g++;

	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind&&f1==0)
			  {
				  f1=1;
				  g--;

			  }

			   in=i+1;
		       h++;

		}
		if(g>0&&h>0)
		{
			cout<<"YES";
			return 0;
		}

   }

   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,ca=-1,cb=-1;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
				h--;

			 ind=i+1;
			 g++;
			 ca++;
	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
				g--;

               cb++;
			   in=i+1;
		       h++;
		}
		if((g>0&&h>0)||(ca>0&&cb>0))
		{
			cout<<"YES";
			return 0;
		}

   }

   cout<<"NO";

     return 0;
 }


#include<bits/stdc++.h>
using namespace std;
int main()
 {   string s,r,hhh;
	 int hh;
	 cin>>s;
	 size_t t=s.size();
	 if(t%2==0)
	  {
		  hh=t/2;
			 r=s.substr(0,hh);
	         hhh=s.substr(hh,t);
	         sort(r.begin(),r.end());
	         sort(hhh.begin(),hhh.end());
		   if (r.compare(hhh) != 0)
                   cout<<"YES";
                   else
                    cout<<"NO";
		//  cout<<"YES";

	  }
	  else
	    {
			hh=t/2;
			 r=s.substr(0,hh);
	         hhh=s.substr(hh+1,t);
	         sort(r.begin(),r.end());
	         sort(hhh.begin(),hhh.end());
	         if (r.compare(hhh) != 0)
                   cout<<"YES";
                   else
                    cout<<"NO";

  }

	 return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,ca=0,cb=0,la=0,lb=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
			{
					h--;
					la++;
			}
		   else
		      ca++;
			 ind=i+1;
			 g++;

	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
			  {
				  g--;
				  lb++;
			  }
            else
               cb++;
			   in=i+1;
		       h++;
		}
		if((g>0&&h>0)||(ca>0&&cb>0)||(la>=2&&lb>=1)||(lb>=2&&la>=1))
		{
			cout<<"YES";
			return 0;
		}

   }

   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,choice=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
			{
				choice++;
				h--;
			}
		   else
		     g++;

			 ind=i+1;
	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
			  {
				  choice++;
				  g--;
			  }
            else
		       h++;
		        in=i+1;

		}
		if(g>0&&h>0)
		{

			cout<<"YES";
			return 0;

		}
		else if((choice%2!=0)&&(choice>=3))
		{
			cout<<"YES";
			return 0;
		}
		else if(((g>0)&&choice>=1)||((h>0)&&choice>=1))
	    {
			cout<<"YES";
			return 0;
		}

   }

   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,ca=0,cb=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
				h--;

			 ind=i+1;
			 g++;
			 ca++;
	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
				g--;

               cb++;
			   in=i+1;
		       h++;
		}
		if(g>0&&h>0)
		{
			cout<<"YES";
			return 0;
		}

   }
if(((ca>=2)&&(cb>=1))||((ca>=2)&&(cb>=1)))
cout<<"YES";
else
   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   s[i]='0';
		   s[i+1]='0';
		   g++;
	   }
   }
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='B'&&s[i+1]=='A')
	   {
		   s[i]='0';
		   s[i+1]='0';
		   h++;
	   }
   }
   if((h>=1)&&(g>=1))
   cout<<"YES";
   else
   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
				h--;
			else
				ind=i+1;

			 g++;

	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
				g--;
			else
			   in=i+1;

		    h++;
		}
		if(g>0&&h>0)
		{
			cout<<"YES";
			return 0;
		}
   }

   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   s[i]='0';
		   s[i+1]='0';
		   g++;
		   break;
	   }
   }
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='B'&&s[i+1]=='A')
	   {
		   s[i]='0';
		   s[i+1]='0';
		   h++;

	   }
   }
   if((h>=1)&&(g>=1))
   cout<<"YES";
   else
   cout<<"NO";

     return 0;
 }


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,choice=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
			choice++;
		   else
		     g++;

			 ind=i+1;
	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
			  choice++;
            else
		       h++;
		        in=i+1;

		}
		if(g>0&&h>0)
		{

			cout<<"YES";
			return 0;

		}
		else if((choice%2!=0)&&(choice>=3))
		{
			cout<<"YES";
			return 0;
		}
		else if(((g>0)&&choice>=1)||((h>0)&&choice>=1))
	    {
			cout<<"YES";
			return 0;
		}

   }

   cout<<"NO";

     return 0;
 }


#include<bits/stdc++.h>
using namespace std;
int main()
 {   string s,r,hhh;
	 long long hh;
	 cin>>s;
	 size_t t=s.size();
	 if(t%2==0)
	  {
		  hh=t/2;
			 r=s.substr(0,hh);
	         hhh=s.substr(hh,t);
	         sort(r.begin(),r.end());
	         sort(hhh.begin(),hhh.end());
		   if (r.compare(hhh) != 0)
                   cout<<"YES";
                   else
                    cout<<"NO";
		//  cout<<"YES";

	  }
	  else
	    {
			hh=t/2;
			 r=s.substr(0,hh);
	         hhh=s.substr(hh+1,t);
	         sort(r.begin(),r.end());
	         sort(hhh.begin(),hhh.end());
	         if (r.compare(hhh) != 0)
                   cout<<"YES";
                   else
                    cout<<"NO";

  }

	 return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
				h--;
			 ind=i+1;
			 g++;

	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
				g--;

			   in=i+1;
		       h++;
		}
		if(g>0&&h>0)
		{
			cout<<"YES";
			return 0;
		}
   }

   cout<<"NO";

     return 0;
 }


#include<bits/stdc++.h>
using namespace std;
int main()
 {   string s,r,hhh;
	 int hh;
	 cin>>s;
	 size_t t=s.size();
	 if(t%2==0)
	  cout<<"YES";
	  else
	    {
			hh=t/2;
			 r=s.substr(0,hh);
	         hhh=s.substr(hh+1,t);
	         sort(r.begin(),r.end());
	         sort(hhh.begin(),hhh.end());
	         if (r.compare(hhh) != 0)
                   cout<<"YES";
                   else
                    cout<<"NO";

  }

	 return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
using namespace std;
void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int main()
{
   fast();
   string s;
   ll g=0,h=0,ind=-10,in=-111,ca=0,cb=0;
   cin>>s;
   ll size=s.size();
   for(ll i = 0 ; i < size-1 ; i++ )
   {
	   if(s[i]=='A'&&s[i+1]=='B')
	   {
		   if(i==in)
				h--;
		   else
		      ca++;
			 ind=i+1;
			 g++;

	   }
	   else if(s[i]=='B'&&s[i+1]=='A')
		{
			if(i==ind)
				g--;
            else
               cb++;
			   in=i+1;
		       h++;
		}
		if((g>0&&h>0)||(ca>0&&cb>0))
		{
			cout<<"YES";
			return 0;
		}

   }

   cout<<"NO";

     return 0;
 }


#include<bits/stdc++.h>

using namespace std;
int ab=INT_MAX,ba=INT_MAX;
int main()
{
    string s;
    cin>>s;
    bool flag=false;
    for(int i=1;s.length()>i;i++)
    {
        if(s[i]=='A' && s[i-1]=='B')
            if(i-1 > ab)
            {
                flag=true;
                break;
            }
            ab=min(ab,i);
        if(s[i]=='B' && s[i+1]=='A')
        {
            if(i-1 > ba)
            {
                flag=true;
                break;
            }
        }
        ba=min(ba,i);
    }
    if(flag)
    {
        cout<<"Yes";
    }
    else
        cout<<"No";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int f1=0,f2=0;
    for(int i=0;s.length()>i;i++)
    {
        if(s[i]=='B')
        {
            if(s[i+1]=='A')
            {
                f2=1;
                s[i]='C';
                s[i+1]='C';
                break;
            }
        }
    }
    for(int i=0;s.length()>i;i++)
    {
        if(s[i]=='A')
        {
            if(s[i+1]=='B')
            {
                f1=1;
                s[i]='C';
                s[i+1]='C';
                break;
            }
        }
    }

    if(f1+f2==2)
        cout<<"YES";
    else
        cout<<"NO";
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    bool flag=false;
    int ab=INT_MAX,ba=INT_MAX;
    for(int i=1;s.length()>=i;i++)
    {
        if(s[i-1]=='A' && s[i]=='B')
        {
            if(i-1 > ab)
            {
                flag=true;
                break;
            }
        }
        ab=min(ab,i);
        if(s[i-1]=='B' && s[i]=='A')
        {
            if(i-1 > ba)
            {
                flag=true;
                break;
            }
        }
        ba=min(ba,i);
    }
    if(flag)
        cout<<"Yes";
    else
        cout<<"No";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int f1=0,f2=0;
    for(int i=0;s.length()>i;i++)
    {
        if(s[i]=='A' && s[i+1]=='B')
        {
            for(int j=i+2;s.length()>j;j++)
            {
                if(s[j]=='B' && s[j+1]=='A')
                {
                    cout<<"YES";
                    return 0;
                }
            }
        }
        else if(s[i]=='B' && s[i+1]=='A')
        {
            for(int j=i+2;s.length()>j;j++)
            {
                if(s[j]=='A' && s[j+1]=='B')
                {
                    cout<<"YES";
                    return 0;
                }
            }
        }
    }
    cout<<"NO";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int f1=0,f2=0;
    for(int i=0;s.length()>i;i++)
    {
        if(s[i]=='A')
        {
            if(s[i-1]=='B')
            {
                f2=1;
                s[i-1]='C';
            }
            if(s[i+1]=='B')
            {
                f1=1;
                s[i+1]='C';
            }
        }
        if(f1==1 && f2==1)
        {
            cout<<"YES";
            return 0;
        }
    }
    cout<<"NO";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int fsub(string s, char r1, char r2)
{
    int len = s.length(),f = 0;
    int i=1;
    while(i<len)
    {
        if(s[i-1] == r1 && s[i] == r2)
        {
            f = true;
            break;
        }
        i++;
    }

    if(f)
    {
        f = false;
        i+=2;
        while(i < len)
        {
            if(s[i-1] == r2 && s[i] == r1)
            {
                f = true;
                break;
            }
            i++;
        }
    }
    return f;
}

int main()
{
    string s;
    cin >> s;

    int f = fsub(s, 'A', 'B');
    if(!f)
        f = fsub(s, 'B', 'A');

    if(f)
        cout << "YES";
    else
        cout << "NO";

    return 0;
}


#include<bits/stdc++.h>

using namespace std;
int ab=INT_MIN,ba=INT_MIN;
int main()
{
    string s;
    cin>>s;
    bool flag=false;
    for(int i=1;s.length()>i;i++)
    {
        if(s[i]=='A' && s[i-1]=='B')
            if(i-1 > ab)
            {
                flag=true;
                break;
            }
            ab=min(ab,i);
        if(s[i]=='B' && s[i+1]=='A')
        {
            if(i-1 > ba)
            {
                flag=true;
                break;
            }
        }
        ba=min(ba,i);
    }
    if(flag)
    {
        cout<<"Yes";
    }
    else
        cout<<"No";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int f1=0,f2=0;
    for(int i=0;s.length()>i;i++)
    {
        if(s[i]=='A')
        {
            if(s[i+1]=='B')
            {
                f1=1;
                s[i]='C';
                s[i+1]='C';
                break;
            }
        }
    }
    for(int i=0;s.length()>i;i++)
    {
        if(s[i]=='B')
        {
            if(s[i+1]=='A')
            {
                f2=1;
                s[i]='C';
                s[i+1]='C';
                break;
            }
        }
    }
    if(f1+f2==2)
        cout<<"YES";
    else
        cout<<"NO";
        return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int i=1,f=0;
    while(i<s.length())
    {
        if(s[i-1]=='A' && s[i]=='B')
        {
            f=1;
            break;
        }
        i++;
    }

    if(f)
    {
        f=0;
        i+=2;
        while(i<s.length())
        {
            if(s[i-1]=='B' && s[i]=='A')
            {
                f=1;
                break;
            }
            i++;
        }
    }
    if(f)
        cout<<"Yes";
    if(!f)
        cout<<"No";

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    string s;
    cin>>s;
    int f1=0,f2=0;
    for(int i=0;s.length()>i;i++)
    {
        if(s[i]=='A' && s[i+1]=='B')
        {
            for(int j=i+2;s.length()>j;j++)
            {
                if(s[j]=='B' && s[j+1]=='A')
                {
                    cout<<"YES";
                    return 0;
                }
            }
        }
        if(s[i]=='B' && s[i+1]=='A')
        {
            for(int j=i+2;s.length()>j;j++)
            {
                if(s[j]=='A' && s[j+1]=='B')
                {
                    cout<<"YES";
                    return 0;
                }
            }
        }
    }
    cout<<"NO";
    return 0;
}


#pragma region Header
#define _CRT_SECURE_NO_WARNINGS
#define print(x) printf("%d\n", x)
#define loop(len) for(int i=0; i<len; ++i)
#define foreach(a) for(int i : a)
#define veci vector<int>
#define vecii vector<long long>
#define pb push_back
#define pp pop_back
#define b begin
#define e end
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include<cmath>
#include<map>
typedef long long  ll;
using namespace std;
#pragma endregion

int main() {
	string str;
	cin >> str;
	bool AB, BA;

	for (int i = 0, j = 1; i < str.size()-1; ++i, ++j) {
		if (str[i] == 'A' && str[j] == 'B') {
			AB = true;
			str[j] = 'C';
		}

		if (str[i] == 'B' && str[j] == 'A') {
			BA = true;
			str[j] = 'C';
		}
	}

	if (AB && BA) {
		printf("YES\n");
	}
	else printf("NO\n");

	return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main() {
	bool Found = false;
	bool A_B = false;
	bool B_A = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		switch (S[i]) {
		case 'A': if (S[i + 1] == 'B') {
			Found = 1; A_B = 1;
				if (S[i + 2] == 'A') {
					Found = 0;
					if (S[i + 3] == 'B')
						Found = 1; i += 3;
				}
		} break;

		case 'B':if (S[i + 1] == 'A') {
			Found = 1; B_A = 1;
			if (S[i + 2] == 'B') {
				Found = 0;
				if (S[i + 3] == 'A')
					Found = 1; i += 3;
			}
		}
		}

	}
	if (Found && B_A && A_B)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main() {
	bool Found = false;
	bool A_B = false;
	bool B_A = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		switch (S[i]) {
		case 'A': if (S[i + 1] == 'B') {
			Found = 1; A_B = 1;
				if (S[i + 2] == 'A') {
					Found = 0;
					if (S[i + 3] == 'B')
						Found = 1;
				}
		} break;

		case 'B':if (S[i + 1] == 'A') {
			Found = 1; B_A = 1;
			if (S[i + 2] == 'B') {
				Found = 0;
				if (S[i + 3] == 'A')
					Found = 1;
			}
		}
		}

	}
	if (Found && B_A && A_B)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main() {
	bool Found = false;
	bool A_B = false;
	bool B_A = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		switch (S[i]) {
		case 'A': if (S[i + 1] == 'B') {
			Found = 1; A_B = 1;
				if (S[i + 2] == 'A') {
					Found = 0; B_A = 1;
					if (S[i + 3] == 'B')
						Found = 1; i += 3;
				}
		} break;

		case 'B':if (S[i + 1] == 'A') {
			Found = 1; B_A = 1;
			if (S[i + 2] == 'B') {
				Found = 0; A_B = 1;
				if (S[i + 3] == 'A')
					Found = 1; i += 3;
			}
		}
		}

	}
	if (Found && B_A && A_B)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#pragma region Header
#define _CRT_SECURE_NO_WARNINGS
#define print(x) printf("%d\n", x)
#define loop(len) for(int i=0; i<len; ++i)
#define foreach(a) for(int i : a)
#define veci vector<int>
#define vecii vector<long long>
#define pb push_back
#define pp pop_back
#define b begin
#define e end
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include<cmath>
#include<map>
typedef long long  ll;
using namespace std;
#pragma endregion

	bool AB ,BA;
int main() {
	string str;
	cin >> str;

	for (int i = 0, j = 1; i < str.size()-1; ++i, ++j) {
		if (str[i] == 'A' && str[j] == 'B') {
			AB = true;
			str[j] = 'C';
		}

		if (str[i] == 'B' && str[j] == 'A') {
			BA = true;
			str[j] = 'C';
		}
	}

	if (AB && BA) {
		printf("YES\n");
	}
	else printf("NO\n");

	return 0;
}


#include <iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
set<int>SET;
int main() {
	bool Found = false;
	bool A_B = false;
	bool B_A = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		switch (S[i]) {
		case 'A': if (S[i + 1] == 'B') {
			Found = 1; A_B = 1;
				if (S[i + 2] == 'A') {
					Found = 0; //B_A = 1;
					if (S[i + 3] == 'B')
						Found = 1; i += 1;
				}
		} break;

		case 'B':if (S[i + 1] == 'A') {
			Found = 1; B_A = 1;
			if (S[i + 2] == 'B') {
				Found = 0; //A_B = 1;
				if (S[i + 3] == 'A')
					Found = 1; i += 1;
			}
		}
		}

	}
	if (Found && B_A && A_B)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#include<iostream>
using namespace std;
int main() {
	bool Found = false;
	bool Overlab = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] == 'A' && S[i + 1] == 'B' || S[i] == 'B' && S[i + 1] == 'A') Found = true;
		if (i != 0 && S[i] == 'B' && S[i - 1] == 'A' && S[i + 1] == 'A') Overlab = true;
		else if (i != 0 && S[i] == 'A' && S[i - 1] == 'B' && S[i + 1] == 'B') Overlab = true;
	}
	if (Found && !Overlab)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#pragma region Header
#define _CRT_SECURE_NO_WARNINGS
#define print(x) printf("%d\n", x)
#define loop(len) for(int i=0; i<len; ++i)
#define foreach(a) for(int i : a)
#define veci vector<int>
#define vecii vector<long long>
#define pb push_back
#define pp pop_back
#define b begin
#define e end
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include<cmath>
#include<map>
typedef long long  ll;
using namespace std;
#pragma endregion

int main() {
	string str;
	cin >> str;
	bool AB, BA;

	for (int i = 0, j = 1; i < str.size()-1; ++i, ++j) {
		if (str[i] == 'A' && str[j] == 'B') {
			AB = true;
			str[j] = 'C';
		}

		if (str[i] == 'B' && str[j] == 'A') {
			AB = true;
			str[j] = 'C';
		}
	}

	if (AB && BA) {
		printf("YES\n");
	}
	else printf("NO\n");

	return 0;
}


#pragma region Header
#define _CRT_SECURE_NO_WARNINGS
#define print(x) printf("%d\n", x)
#define loop(len) for(int i=0; i<len; ++i)
#define foreach(a) for(int i : a)
#define veci vector<int>
#define vecii vector<long long>
#define pb push_back
#define pp pop_back
#define b begin
#define e end
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include<cmath>
#include<map>
typedef long long  ll;
using namespace std;
#pragma endregion

	bool AB ,BA;
int main() {
	string str;
	cin >> str;

	for (int i = 0, j = 1; i < str.size()-1; ++i, ++j) {
		if (str[i] == 'A' && str[j] == 'B' && !AB) {
			AB = true;
			str[j] = 'C';
		}

		if (str[i] == 'B' && str[j] == 'A' && !BA) {
			BA = true;
			str[j] = 'C';
		}
	}

	if (AB && BA) {
		printf("YES\n");
	}
	else printf("NO\n");

	return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main() {
	bool Found = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		switch (S[i]) {
		case 'A': if (S[i + 1] == 'B') {
			Found = 1;
				if (S[i + 2] == 'A') {
					Found = 0;
					if (S[i + 3] == 'B')
						Found = 1; i += 3;
				}
		} break;

		case 'B':if (S[i + 1] == 'A') {
			Found = 1;
			if (S[i + 2] == 'B') {
				Found = 0;
				if (S[i + 3] == 'A')
					Found = 1; i += 3;
			}
		}
		}

	}
	if (Found)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main() {
	bool Found = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] == 'A' && S[i + 1] == 'B' || S[i] == 'B' && S[i + 1] == 'A') { Found = true; i++; }
		else if (Found == true && S[i] == 'A' || S[i] == 'B') Found = false;

	}
	if (Found)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#include <iostream>
#include<string>
using namespace std;
int main() {
	bool Found = false;
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] == 'A' && S[i + 1] == 'B' || S[i] == 'B' && S[i + 1] == 'A') { Found = true; i++; }
		else if (S[i] != 'A' && S[i] != 'B' && Found) break;
		else if (Found && (S[i] == 'A' || S[i] == 'B')) Found = false;

	}
	if (Found)
		cout << "YES\n";
	else cout << "NO\n";

	return 0;
}


#include <iostream>
#include <string>
using namespace std;

string s;
bool AB, BA;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> s;

	int i = s.find("AB");
	if (i != -1)
		AB = true;
	int j = s.find("BA", i+2);
	if (j != -1)
		BA = true;

	if (AB && BA)
		return cout << "YES\n",0;

	AB = BA = false;

	i = s.find("BA");
	if (i != -1)
		AB = true;
	j = s.find("AB", i + 2);
	if (j != -1)
		BA = true;

	if (AB && BA)
		cout << "YES\n";
	else
		cout << "NO\n";

	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;

string s;

int main(){
    cin>>s;
    int f1 = 0, f2 = 0;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A' && !f1){
            f1 = 1;
            i++;
        }
        if(f1 && s[i] == 'B' && s[i-1] == 'A')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES");
        return 0;
    }

    f1 = f2 = 0;

    FOR(i, 1, s.size()){
        if(s[i-1] == 'A' && s[i] == 'B' && !f1){
            f1 = 1;
            i++;
        }
        if(f1 && s[i] == 'A' && s[i-1] == 'B')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES");
        return 0;
    }
    printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    FOR(i, 2, s.size()){
        if ((s[i-2] == 'A' && s[i-1] == 'B' && s[i] == 'A')||(s[i-2] == 'B' && s[i-1] == 'A' && s[i] == 'B')){
            printf("NO");
            return 0;
        }
    }
    FOR(i, 1, s.size()){
        if((s[i-1] == 'B' && s[i] == 'A')||(s[i-1] == 'A' && s[i] == 'B')){
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    return 0;
}


By rav.gupta, contest: Codeforces Round #306 (Div. 2), problem: (A) Two Substrings, Wrong answer on test 52, #
 #include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;

string s;

int main(){
    cin>>s;
    int f1 = 0, f2 = 0;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A' && !f1){
            f1 = 1;
            i++;
        }
        if(f1 && s[i] == 'B' && s[i-1] == 'A')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES");
        return 0;
    }

    f1 = f2 = 0;

    FOR(i, 1, s.size()){
        if(s[i-1] == 'A' && s[i] == 'B' && !f1){
            f1 = 1;
            i++;
        }
        if(f1 && s[i] == 'A' && s[i-1] == 'B')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES");
        return 0;
    }
    printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0;
    FOR(i, 1, s.size()){
        if((s[i-1] == 'B' && s[i] == 'A'){
            f1 = 1;
            i++;
        }
        if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
            i++;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0, j = -1;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A'){
            f1++;
            j = i;
            break;
        }
    }
    FOR(i, 1, s.size()){
    	if((i == j+1 || i == j-1) &&(f1 ==1)) continue;
    	if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0, j = -1;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A'){
            f1++;
            j = i;
        }
    }
    FOR(i, 1, s.size()){
    	if((i == j+1 || i == j-1) &&(f1 ==1)) continue;
    	if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0, j = -1;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A'){
            f1 = 1;
            j = i;
        }
    }
    FOR(i, 1, s.size()){
    	if(i == j+1 || i == j-1) continue;
    	if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


By rav.gupta, contest: Codeforces Round #306 (Div. 2), problem: (A) Two Substrings, Wrong answer on test 52, #
 #include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0, j = -1;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A'){
            f1++;
            j = i;
        }
    }
    FOR(i, 1, s.size()){
    	if((i == j+1 || i == j-1) &&(f1 ==1)) continue;
    	if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0, j = -1;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A'){
            f1 = 1;
            j = i;
        }
    }
    FOR(i, 1, s.size()){
    	if(i == j+1) continue;
    	if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int cnt =0;
    FOR(i, 1, s.size()){
        if((s[i-1] == 'B' && s[i] == 'A')||(s[i-1] == 'A' && s[i] == 'B')){
            cnt++;
            i++;
        }
    }
    if(cnt > 1) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;

string s;

int main(){
    cin>>s;
    int f1 = 0, f2 = 0;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A' && !f1){
            f1 = 1;
            i++;
        }
        if(f1 && s[i] == 'B' && s[i-1] == 'A')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES");
        return 0;
    }

    f1 = f2 = 0;

    FOR(i, 1, s.size()){
        if(s[i-1] == 'A' && s[i] == 'B' && !f1){
            f1 = 1;
            i++;
        }
        if(f1 && s[i] == 'A' && s[i-1] == 'B')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES*");
        return 0;
    }
    printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    FOR(i, 2, s.size()){
        if ((s[i-2] == 'A' && s[i-1] == 'B' && s[i] == 'A')||(s[i-2] == 'B' && s[i-1] == 'A' && s[i] == 'B')){
            printf("NO");
            return 0;
        }
    }
    printf("YES");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A'){
            f1 = 1;
            i++;
        }
        if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
            i++;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    int f1 = 0, f2 = 0;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A'){
            f1 = 1;
        }
    }
    FOR(i, 1, s.size()){
    	if(s[i-1] == 'A' && s[i] == 'B'){
            f2 = 1;
        }
    }
    if(f1 && f2) printf("YES");
    else printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;
string s;
int main(){
    cin>>s;
    FOR(i, 2, s.size()){
        if ((s[i-2] == 'A' && s[i-1] == 'B' && s[i] != 'A')||(s[i-2] == 'B' && s[i-1] == 'A' && s[i] != 'B')){
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define FORR(i, a, b) for(int i = a;i>b; i=i-1)
#define ri(x) scanf("%d", &x)
#define rl(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
using namespace std;

typedef long long ll;
typedef long double dbl;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
const int INF = 2e6;
const ll LINF = 8e18;
const int MOD = 1e9 + 7;
const dbl EPS = 1e-9;

string s;

int main(){
    cin>>s;
    int f1 = 0, f2 = 0;
    FOR(i, 1, s.size()){
        if(s[i-1] == 'B' && s[i] == 'A' && !f1){
            f1 = 1;
            i++;
        }
        else if(f1 && s[i] == 'B' && s[i-1] == 'A')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES");
        return 0;
    }

    f1 = f2 = 0;

    FOR(i, 1, s.size()){
        if(s[i-1] == 'A' && s[i] == 'B' && !f1){
            f1 = 1;
            i++;
        }
        else if(f1 && s[i] == 'A' && s[i-1] == 'B')
            f2 = 1;
    }

    if(f1 && f2){
        printf("YES");
        return 0;
    }
    printf("NO");
    return 0;
}


#include <bits/stdc++.h>
#define NN 101100

using namespace std;

char s[NN];

main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif

	cin>>s;

	int cnt1=0,cnt2=0,n=strlen(s);

	for(int i=0;i<n-1;i++){
		if(s[i]=='A' && s[i+1]=='B'){
			for(int j=i+2;j<n-1;j++){
				if(s[j]=='B' && s[j+1]=='A'){
					puts("YES");
					return 0;
				}
			}
		}
	}

	for(int i=0;i<n-1;i++){
		if(s[i]=='B' && s[i+1]=='A'){
			for(int j=i+2;j<n-1;j++){
				if(s[j]=='A' && s[j+1]=='B'){
					puts("YES");
					return 0;
				}
			}
		}
	}

	puts("NO");
	return 0;
}


#include <bits/stdc++.h>
#define NN 100100
using namespace std;
typedef long long INT;
char str[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	cin>>str;
	int L = strlen(str);
	int cnt1 = 0, cnt2 = 0;
	for(int i = 0; i < L - 1; i++){
		if(str[i] == 'A' && str[i + 1] == 'B'){
			cnt1++;
			i++;
			continue;
		}
		if(str[i] == 'B' && str[i + 1] == 'A'){
			cnt2++;
			i++;
		}
	}
	if(cnt1 == 1 && cnt2 == 1) puts("YES");
	else puts("NO");
	return 0;
}


#include <bits/stdc++.h>
#define NN 100110

using namespace std;

char s[NN];

main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif

	cin>>s;

	int cnt1=0,cnt2=0,n=strlen(s);

	for(int i=0;i<n-1;i++){
		if(s[i]=='A' && s[i+1]=='B'){
			cnt1++;
			s[i]='*';
			s[i+1]='*';
			break;
		}
	}

	for(int i=0;i<n-1;i++){
		if(s[i]=='B' && s[i+1]=='A'){
			cnt2++;
			break;
		}
	}

	if(cnt1&cnt2)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
char s[100010];
int main() {
#ifndef ONLINE_JUDGE
	freopen("16in.in", "r", stdin);
	freopen("16out.out", "w", stdout);
#endif
	cin>>s;
	int f=0;
	int l=strlen(s);
	for(int i=0;!f and i<l;i++){
		if(s[i]=='A' and s[i+1]=='B'){
			for(int j=i+2;j<l;j++){
				if(s[j]=='B' and s[j+1]=='A'){f=1;break;}
			}
		}
		else if(s[i]=='B' and s[i+1]=='A'){
			for(int j=i+2;j<l;j++){
				if(s[j]=='A' and s[j+1]=='B'){f=1;break;}
			}
		}
	}
	if(f) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NN 11111111
char a[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	gets(a);
	int l=strlen(a),k=l;
	for(int i=0;i<l;i++){
		if((a[i]=='A'&&a[i+1]=='B')||(a[i]=='B'&&a[i+1]=='A')){
			k=i;
			break;
		}
	}
	for(int i=k+2;i<l;i++){
		if((a[k]=='A'&&a[i]=='B'&&a[i+1]=='A')||(a[k]=='B'&&a[i]=='A'&&a[i+1]=='B')){
			puts("YES");
			return 0;
		}
	}
	puts("NO");
}


#include<bits/stdc++.h>
#define NN 100050

using namespace std;

char s[NN];
int f[NN]={0,};
int main() {
#ifndef	ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	int n,ab=0,ba=0;
	scanf("%s",s);
	n=strlen(s);
	for(int i=0;i<n;i++){
		if(s[i]=='A' and s[i+1]=='B')if(f[i]==0 and f[i+1]==0){ab++;f[i]=f[i+1]=1;}
		if(s[i]=='B' and s[i+1]=='A')if(f[i]==0 and f[i+1]==0){ba++;f[i]=f[i+1]=1;}
	}
	if(ab and ba)puts("YES");
	else puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NN 11111111
char a[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	gets(a);
	int l=strlen(a),k=l;
	for(int i=0;i<l-1;i++){
		if((a[i]=='A'&&a[i+1]=='B')||(a[i]=='B'&&a[i+1]=='A')){
			k=i;
			break;
		}
	}
	for(int i=k+2;i<l-1;i++){
		if((a[k]=='A'&&a[i]=='B'&&a[i+1]=='A')||(a[k]=='B'&&a[i]=='A'&&a[i+1]=='B')){
			puts("YES");
			return 0;
		}
	}
	puts("NO");
}


#include<bits/stdc++.h>
#define NN 110000
using namespace std;

char st[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>st;
	int n=strlen(st);
	int a=0,b=0;
	int id=0;
	for(int i=0;i<n;i++){
		if(st[i]=='A' and st[i+1]=='B'){
			a=1;
			i+=2;
		}
		if(st[i]=='B' and st[i+1]=='A'){
			b=1;
			i+=2;
		}
	}
	if(a==1 and b==1) puts("YES");
	else puts("NO");
	return 0;
}


#include <bits/stdc++.h>
#define NN 10110

using namespace std;

char s[NN];

main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif

	cin>>s;

	int cnt1=0,cnt2=0,n=strlen(s);

	for(int i=0;i<n-1;i++){
		if(s[i]=='A' && s[i+1]=='B'){
			cnt1++;
			s[i]='*';
			s[i+1]='*';
			break;
		}
	}

	for(int i=0;i<n-1;i++){
		if(s[i]=='B' && s[i+1]=='A'){
			cnt2++;
			break;
		}
	}

	if(cnt1&cnt2)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define NN 100100
using namespace std;
typedef long long INT;
int f[NN][2], g[NN][2];
char str[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	cin>>str;
	int L = strlen(str);
	int cnt = 0;
	for(int i = 0; i < L - 1; i++){
		if(str[i] == 'A' && str[i + 1] == 'B'){
			for(int j = i + 2; j < L - 1; j++){
				if(str[j] == 'B' && str[j + 1] == 'A'){
					puts("YES");
					return 0;
				}
			}
			cnt++;
		}
		if(str[i] == 'B' && str[i + 1] == 'A'){
			for(int j = i + 2; j < L - 1; j++){
				if(str[j] == 'A' && str[j + 1] == 'B'){
					puts("YES");
					return 0;
				}
			}
			cnt++;
		}
		if(cnt == 2) break;
	}
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char a[100100];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
//	freopen("out.out","w",stdout);
#endif
	cin>>a;
	int n=strlen(a);
	for(int i=0;i<n;i++){
		if(a[i]=='A' and a[i+1]=='B') {
			for(int j=i+2;j<n;j++){
				if(a[j]=='B' and a[j+1]=='A') {
					puts("YES");
					return 0;
				}
			}
		}
		if(a[i]=='B' and a[i+1]=='A'){
			for(int j=i+2;j<n;j++){
				if(a[j]=='A' and a[j+1]=='B') {
					puts("YES");
					return 0;
				}
			}
		}
	}
	puts("NO");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
char s[100010];
int main() {
#ifndef ONLINE_JUDGE
	freopen("16in.in", "r", stdin);
	freopen("16out.out", "w", stdout);
#endif
	cin>>s;
	int f=0,g=0;
	int l=strlen(s);
	for(int i=0;i<l;i++){
		if(s[i]=='A' and s[i+1]=='B'){
			f=1;
			i+=2;
		}
		if(f and s[i]=='B' and s[i+1]=='A'){
			g=1;
			i++;
		}
	}
	if(f and g) {cout<<"YES"<<endl;return 0;}
	f=0,g=0;
	for(int i=0;i<l;i++){
		if(s[i]=='B' and s[i+1]=='A'){
			g=1;
			i+=2;
		}
		if(g and s[i]=='A' and s[i+1]=='B'){
			f=1;
			i++;
		}
	}
	if(f and g) {cout<<"YES"<<endl;return 0;}
	cout<<"NO"<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
char s[100010];
int main() {
#ifndef ONLINE_JUDGE
	freopen("16in.in", "r", stdin);
	freopen("16out.out", "w", stdout);
#endif
	cin>>s;
	int f=0,g=0;
	int l=strlen(s);
	for(int i=0;i<l;i++){
		if(s[i]=='A' and s[i+1]=='B'){
			f=1;
			i++;
		}
		else if(s[i]=='B' and s[i+1]=='A'){
			g=1;
			i++;
		}
	}
	if(f and g) {cout<<"YES"<<endl;return 0;}
	f=0,g=0;
	for(int i=0;i<l;i++){
		if(s[i]=='B' and s[i+1]=='A'){
			g=1;
			i++;
		}
		else if(s[i]=='A' and s[i+1]=='B'){
			f=1;
			i++;
		}
	}
	if(f and g) {cout<<"YES"<<endl;return 0;}
	cout<<"NO"<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define nn 1000000
char a[nn];
main(){
	cin>>a;
	int k=strlen(a),c=0;
	for(int i=0;i<k-1;i++){
		if(a[i]=='A'&&a[i+1]=='B'){
			for(int j=i+2;j<k-1;j++){
				if(a[j]=='B' and a[j]=='A'){
					puts("YES");
					return 0;
				}
			}
			c++;
		}
		if(a[i]=='B'&&a[i+1]=='A'){
			for(int j=i+2;j<k-1;j++){
				if(a[j]=='A' and a[j]=='B'){
					puts("YES");
					return 0;
				}
			}
			c++;
		}
		if(c==2) break;
	}
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NN 11111111
char a[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	gets(a);
	int l=strlen(a),flag=0,k;
	for(int i=0;i<l;i++){
		if((a[i]=='A'&&a[i+1]=='B')||(a[i]=='B'&&a[i+1]=='A')){
			flag=1;
			k=i;
			break;
		}
	}
	for(int i=k+2;i<l;i++){
		if((a[k]=='A'&&a[i]=='B'&&a[i+1]=='A')||(a[k]=='B'&&a[i]=='A'&&a[i+1]=='B')){
			puts("YES");
			flag=2;
			return 0;
		}
	}
	if(flag==0||flag==1){
		puts("NO");
		return 0;
	}
}


#include <bits/stdc++.h>
#define NN 101100

using namespace std;

char s[NN];

main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif

	cin>>s;

	int cnt1=0,cnt2=0,n=strlen(s);

	for(int i=0;i<n-1;i++){
		if(s[i]=='A' && s[i+1]=='B'){
			if(cnt1)continue;
			for(int j=i+2;j<n-1;j++){
				if(s[j]=='B' && s[j+1]=='A'){
					puts("YES");
					return 0;
				}
			}
			cnt1++;
		}

		if(s[i]=='B' && s[i+1]=='A'){
			if(cnt2)continue;
			for(int j=i+2;j<n-1;j++){
				if(s[j]=='A' && s[j+1]=='B'){
					puts("YES");
					return 0;
				}
			}
			cnt2++;
		}
		if(cnt1 && cnt2)break;
	}

	puts("NO");
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;

#define NN 100010
char s[NN];
vector<int> a,b;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	scanf("%s",s+1);
	int len=strlen(s+1);
	for(int i=1;i<len;i++){
		if(s[i]=='A'&&s[i+1]=='B') a.pb(i);
		if(s[i]=='B'&&s[i+1]=='A') b.pb(i);
	}
	if(!a.size()){
		puts("NO");
		return 0;
	}
	if(!b.size()){
		puts("NO");
		return 0;
	}

	for(int i=0;i<a.size();i++){
		int id=a[i];
		int x=upper_bound(b.begin(),b.end(),id+1)-b.begin();
		if(x<b.size()){
			puts("YES");
			return 0;
		}
		x=lower_bound(b.begin(),b.end(),id-1)-b.begin();
		x--;
		if(x>=0){
			puts("YES");
			return 0;
		}
	}
	puts("NO");
    return 0;
}


#include<bits/stdc++.h>
#define NN 110000
using namespace std;
int a[NN];
int b[NN];
char st[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>st;
	int n=strlen(st);
	int id=0,td=0;
	for(int i=0;i<n;i++){
		if(st[i]=='A' and st[i+1]=='B')a[id++]=i;
		if(st[i]=='B' and st[i+1]=='A')b[td++]=i;
	}
	if(id==0 or td==0)puts("NO");
	else if(abs(a[id-1]-b[0])>=2 or abs(a[0]-b[td-1])>=2) puts("YES");
	else puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int,int> pii;
char a[100100];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>a;
	int n=strlen(a);
	int f=0,g=0;
	for(int i=0;i<n;i++){
		if(a[i]=='A' and a[i+1]=='B') {
			for(int j=i+2;j<n;j++){
				if(a[j]=='B' and a[j+1]=='A') {
					puts("YES");
					return 0;
				}
			}
			f++;
		}
		if(a[i]=='B' and a[i+1]=='A'){
			for(int j=i+2;j<n;j++){
				if(a[j]=='A' and a[j+1]=='B') {
					puts("YES");
					return 0;
				}
			}
			f++;
		}
		if(f==2) break;
	}
	puts("NO");
	return 0;
}


#include <bits/stdc++.h>
#define NN 10110

using namespace std;

char s[NN];

main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
#endif

	cin>>s;

	int cnt1=0,cnt2=0,n=strlen(s);

	for(int i=0;i<n-1;i++){
		if(s[i]=='A' && s[i+1]=='B'){
			for(int j=i+2;j<n-1;j++){
				if(s[j]=='B' && s[j+1]=='A'){
					puts("YES");
					return 0;
				}
			}
		}
	}

	for(int i=0;i<n-1;i++){
		if(s[i]=='B' && s[i+1]=='A'){
			for(int j=i+2;j<n-1;j++){
				if(s[j]=='A' && s[j+1]=='B'){
					puts("YES");
					return 0;
				}
			}
		}
	}

	puts("NO");
	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
#define mod 1000000007
#define pb push_back
#define inf 0x3f3f3f3f
#define bclz(x) (__builtin_clz(x))
#define bctz(x) (__builtin_ctz(x))

using namespace std;

typedef long long INT;
typedef pair<int, int> pii;
typedef pair<pii, int> pi3;
typedef double DO;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

template<typename T> inline void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}
template<class T, class T1>inline void gn(T &x, T1 &y) {gn(x); gn(y);}
template<class T, class T1, class T2>inline void gn(T &x, T1 &y, T2 &z) {gn(x); gn(y); gn(z);}
template<typename T> inline void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}
template<typename T> inline void println(T x) {print(x), putchar('\n');}
template<typename T> inline void printsp(T x) {print(x), putchar(' ');}
template<class T, class T1> inline void print(T x, T1 y) {printsp(x), println(y);}
template<class T, class T1, class T2> inline void print(T x, T1 y, T2 z) {printsp(x), printsp(y), println(z);}

int power(int a, int b, int m, int ans=1) {
	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;
	return ans;
}

#define NN 100010
char str[NN];
int f[NN][2], b[NN][2];

int main () {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>(str+1);
	int len=strlen(str+1);
	for (int i=1; i<len; i++) if (str[i]=='A' and str[i+1]=='B') {
		for (int j=i+1; j<=len; j++) f[j][0]=1;
		break;
	}
	for (int i=1; i<len; i++) if (str[i]=='B' and str[i+1]=='A') {
		for (int j=i+1; j<=len; j++) f[j][1]=1;
		break;
	}
	for (int i=len; i>1; i--) if (str[i]=='B' and str[i-1]=='A') {
		for (int j=i-1; j>=1; j--) b[j][0]=1;
		break;
	}
	for (int i=len; i>1; i--) if (str[i]=='A' and str[i-1]=='B') {
		for (int j=i-1; j>=1; j--) b[j][1]=1;
	}
	for (int i=1; i<=len; i++) {
		if (str[i]=='A' and str[i+1]=='B') {
			if (f[i-1][1] or b[i+2][1]) return puts("YES");
		}
		if (str[i]=='B' and str[i+1]=='A') {
			if (f[i-1][0] or b[i+2][0]) return puts("YES");
		}
	}
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define NN 11111111
char a[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
#endif
	gets(a);
	int l=strlen(a),k=l;
	for(int i=0;i<l;i++){
		if(a[i]=='A'&&a[i+1]=='B'){
			for(int j=i+2;j<l;j++){
				if(a[j]=='B'&&a[j+1]=='A'){
					puts("YES");
					return 0;
				}
			}
		}
		if(a[i]=='B'&&a[i+1]=='A'){
			for(int j=i+2;j<l;j++){
				if(a[j]=='A'&&a[j+1]=='B'){
					puts("YES");
					return 0;
				}
			}
		}
	}
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int,int> pii;
char a[100100];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>a;
	int n=strlen(a);
	int f=0,g=0;
	for(int i=0;i<n;i++){
		if(a[i]=='A' and a[i+1]=='B') {f++;break;}
	}
	for(int i=0;i<n;i++){
		if(a[i]=='B' and a[i+1]=='A') {g++;break;}
	}
	if(f==0 or g==0) {puts("NO");return 0;}
	if(f>1 or g>1) {puts("YES");return 0;}
	for(int i=0;i<n;i++){
		if(a[i]=='A' and a[i+1]=='B' and a[i+2]=='A') {puts("NO");return 0;}
		if(a[i]=='B' and a[i+1]=='A' and a[i+2]=='B') {puts("NO");return 0;}
	}
	puts("YES");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define nn 1000000
char a[nn];
main(){
	cin>>a;
	int k=strlen(a),c=0;
	for(int i=0;i<k-1;i++){
		if(a[i]=='A'&&a[i+1]=='B'){
			for(int j=i+2;j<k-1;j++){
				if(a[j]=='B' and a[j+1]=='A'){
					puts("YES");
					return 0;
				}
			}
			c++;
		}
		if(a[i]=='B'&&a[i+1]=='A'){
			for(int j=i+2;j<k-1;j++){
				if(a[j]=='A' and a[j+1]=='B'){
					puts("YES");
					return 0;
				}
			}
			c++;
		}
		if(c==2) break;
	}
	puts("NO");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p = s.find("AB");
    if(abs(t-p)>1 && abs(t-p)<s.length())
    cout<<"YES";
    else
    cout << "NO";
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =1000000000;
    if(t+2<s.length())
    p = s.find("BA",t+2,s.length());
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=1000000000;
      if(j+2<s.length())
      k = s.find("AB",j+2,s.length());
      if(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p = s.find("BA",t+2,s.length());
    if(abs(t-p)>1 && abs(t-p)<s.length())
    cout<<"YES";
    else
    {
      int j = s.find("BA");
      int k = s.find("AB",j+2,s.length());
      if(abs(j-k)>1 && abs(j-k)<s.length());
      cout << "YES";
      else
      cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =-1000000000;
    char ch[]="BA";
    if(t+2<s.length())
      p = s.find(ch,t+2);

    if((p-t)>1 && abs(t-p)<s.length() &&p!=-1 && t!=-1){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=-1000000000;
      if(j+2<s.length()){
      k = s.find("AB",j+2);

      }
      if(k==-1||j==-1){
        cout << "NO";
        return 0;
      }
      if(k-j>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =100000000000000;
    if(t+2<s.length())
    int p = s.find("BA",t+2,s.length());
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=100000000000000;
      if(j+2<s.length())
      k = s.find("AB",j+2,s.length());
      if(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    if(t+2<s.length())
    int p = s.find("BA",t+2,s.length());
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      if(j+2<s.length())
      int k = s.find("AB",j+2,s.length());
      if(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =1000000000;
    if(t+2<s.length())
    p = s.find("BA",t+2);
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=1000000000;
      if(j+2<s.length()){
      k = s.find("AB",j+2);

      }
      If(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =-1000000000;
    char ch[]="BA";
    if(t+2<s.length())
      p = s.find(ch,t+2);
   if(p==-1||t==-1){
    cout<<"NO";
    return 0;
   }

    if((p-t)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=-1000000000;
      if(j+2<s.length()){
      k = s.find("AB",j+2);

      }
      if(k==-1||j==-1){
        cout << "NO";
        return 0;
      }
      if(k-j>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =1000000000;
    if(t+2<s.length())
    p = s.find("BA",t+2,s.length());
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");

      int k = s.find("AB",j+2,s.length());
      if(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s,t="AB";
    cin >> s;
    int j=0,p=0;
    for(int i=0;i<s.length();i++){
        if(s[i]==t[j])
        {
          j++;
          p++;
          if(j!=2)
           i = -1;
           else
           i =i+1;
        }
        if(s[i]!=t[j])
          j=0;
        if(j==2)
        {

          if(s[i]==s[j]){
              j--;
              p++;
              if(p==4)
               break;
          }
        }

    }
    if(p==4)
    printf("YES");
    else
    printf("NO");
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =0;
    if(t+2<s.length())
    int p = s.find("BA",t+2,s.length());
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=0;
      if(j+2<s.length())
      k = s.find("AB",j+2,s.length());
      if(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =1000000000;
    if(t+2<s.length()-2)
    p = s.find("BA",t+2,s.length());
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=1000000000;
      if(j+2<s.length()-2)
      k = s.find("AB",j+2,s.length());
      if(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =1000000000;
    if(t+2<s.length())
    p = s.find("BA",t+2,s.length());
    if(fabs(t-p)>1 && fabs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=-1000000000;
      if(j+2<s.length())
      k = s.find("AB",j+2,s.length());
      if(k-j>1 && fabs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int t = s.find("AB");
    int p =1000000000;
    if(t+2<s.length())
    p = s.find("BA",t+2);
    if(abs(t-p)>1 && abs(t-p)<s.length()){
        cout<<"YES";
    }
    else{
      int j = s.find("BA");
      int k=1000000000;
      if(j+2<s.length()){
      k = s.find("AB",j+2;

      }
      If(abs(j-k)>1 && abs(j-k)<s.length())
        cout << "YES";
      else
        cout << "NO";
    }

}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN], curGcd;

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}
string solve() {

	curGcd = 1;
	for (int i = 0; i < n; i++)
		curGcd = gcd(curGcd, a[i]);
	for (int i = 0; i < n; i++)
		a[i] /= curGcd;
	for (int i = 0; i < n; i++)
		if (a[i] != 1 and (a[i] % 2 != 0) and (a[i] % 3 != 0))
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << solve() << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN], curGcd;

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}
string solve() {
	curGcd = 0;
	for (int i = 0; i < n; i++)
		curGcd = gcd(curGcd, a[i]);
	for (int i = 0; i < n; i++)
		a[i] /= curGcd;
	for (int i = 0; i < n; i++)
		if ((a[i] % 2 != 0) and (a[i] % 3 != 0))
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << solve() << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN], curGcd;

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}
string solve() {

	curGcd = gcd(a[0], a[1]);
	for (int i = 1; i < n; i++)
		curGcd = gcd(curGcd, a[i]);
	cout << curGcd << endl;
	for (int i = 0; i < n; i++)
		a[i] /= curGcd;
	for (int i = 0; i < n; i++)
		if (a[i] != 1 and (a[i] % 2 != 0) and (a[i] % 3 != 0))
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	//cout << solve() << endl;
	cout << gcd(5, 15) << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN];

string solve() {

	for (int i = 0; i < n; i++) {
		while (a[i] % 2 == 0)
			a[i] /= 2;
		while (a[i] % 3 == 0)
			a[i] /= 3;
	}
	for (int i = 1; i < n; i++)
		if (a[i] != a[i - 1])
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << solve() << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN], curGcd;

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}
string solve() {

	curGcd = gcd(a[0], a[1]);
	for (int i = 1; i < n; i++)
		curGcd = gcd(curGcd, a[i]);
	for (int i = 0; i < n; i++)
		a[i] /= curGcd;
	for (int i = 0; i < n; i++)
		if (a[i] != 1 and (a[i] % 2 != 0) and (a[i] % 3 != 0))
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << solve() << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN], curGcd;

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}
string solve() {
	curGcd = 1;
	for (int i = 0; i < n; i++)
		curGcd = gcd(curGcd, a[i]);
	for (int i = 0; i < n; i++)
		a[i] /= curGcd;
	for (int i = 0; i < n; i++)
		if ((a[i] % 2 != 0) and (a[i] % 3 != 0))
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << solve() << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN], curGcd;

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}
string solve() {
	curGcd = gcd(a[0], a[1]);
	for (int i = 1; i < n; i++)
		curGcd = gcd(curGcd, a[i]);
	for (int i = 0; i < n; i++)
		a[i] /= curGcd;
	for (int i = 0; i < n; i++)
		if ((a[i] % 2 != 0) and (a[i] % 3 != 0))
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << solve() << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define LL long long
const int MAXN = 1e5 + 1;
int n, a[MAXN], curGcd;

int gcd(int a, int b) {
	if (a == 0) return b;
	return gcd(b % a, a);
}
string solve() {

	curGcd = gcd(a[0], a[1]);
	for (int i = 1; i < n; i++)
		curGcd = gcd(curGcd, a[i]);
	cout << curGcd << endl;
	for (int i = 0; i < n; i++)
		a[i] /= curGcd;
	for (int i = 0; i < n; i++)
		if (a[i] != 1 and (a[i] % 2 != 0) and (a[i] % 3 != 0))
			return "No";
	return "Yes";
}
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cout << solve() << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	ll bids[n];
	for(int i=0;i<n;i++){
		scanf("%Ld",bids+i);
	}
	sort(bids, bids+n);

	ll result = bids[0];
	for(int i = 1; i < n; i++){
    	result = __gcd(result, bids[i]);
	}
	//printf("GCD %Ld\n",result);
	bool flag = true;

	for(int i=0;i<n;i++){
		bids[i] = bids[i]/result;
	}
	for(int i=0;i<n;i++){
		if(bids[i]%2 != 0 && bids[i]%3!=0){
			flag = false;
			break;
		}
	}
	if(flag == false){
		printf("No");
	}
	else{
		printf("Yes");
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	ll bids[n];
	for(int i=0;i<n;i++){
		scanf("%Ld",bids+i);
	}
	for(int i=0;i<n;i++){
		while(bids[i]%2 == 0){
			bids[i] = bids[i]/2;
			}
		while(bids[i]%3==0){
			bids[i] = bids[i]/3;
			}
	}

	int temp = bids[0];
	for(int i=0;i<n;i++){
		if(bids[i] != temp){
			printf("No\n");
			return 0;
		}
	}
	printf("Yes\n");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	int bids[n];
	for(int i=0;i<n;i++){
		scanf("%d",bids+i);
	}
	for(int i=0;i<n;i++){
		while(bids[i]%2 == 0){
			bids[i] = bids[i]/2;
		}
		while(bids[i]%3 == 0){
			bids[i] = bids[i]/3;
		}
	}
	sort(bids, bids+n);
	if(bids[0] == bids[n-1]){
		printf("Yes\n");
	}
	else{
		printf("No\n");
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	ll bids[n];
	for(int i=0;i<n;i++){
		scanf("%Ld",bids+i);
	}
	for(int i=0;i<n;i++){
		while(bids[i]%2==0 || bids[i]%3==0){
			if(bids[i]%2 == 0){
				bids[i] = bids[i]/2;
			}
			else if(bids[i]%3==0){
				bids[i] = bids[i]/3;
			}
		}
	}
	int temp = bids[0];
	for(int i=0;i<n;i++){
		if(bids[i] != temp){
			printf("No\n");
			return 0;
		}
	}
	printf("Yes\n");
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	ll bids[n];
	for(int i=0;i<n;i++){
		scanf("%Ld",bids+i);
	}
	sort(bids, bids+n);

	ll result = bids[0];
	for(int i = 1; i < n; i++){
    	result = __gcd(result, bids[i]);
	}
	//printf("GCD %Ld\n",result);
	bool flag = true;

	for(int i=0;i<n;i++){
		bids[i] = bids[i]/result;
	}
	for(int i=0;i<n;i++){
		if(bids[i]%2 != 0 && bids[i]%3!=0){
			flag = false;
			break;
		}
	}
	if(flag == false){
		printf("No\n");
	}
	else{
		printf("Yes\n");
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	ll bids[n];
	for(int i=0;i<n;i++){
		scanf("%Ld",bids+i);
	}
	for(int i=0;i<n;i++){

		while(bids[i]%2 == 0){
			bids[i] = bids[i]/2;
			}
		while(bids[i]%3 == 0){
			bids[i] = bids[i]/3;
			}
	}
	sort(bids, bids+n);
	if(bids[0] == bids[n-1]){
		printf("Yes");
	}
	else{
		printf("No");
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
typedef long long int ll;
using namespace std;

int main(){
	int n;
	scanf("%d",&n);
	int bids[n];
	for(int i=0;i<n;i++){
		scanf("%d",bids+i);
	}
	for(int i=0;i<n;i++){
		while(bids[i]%2 == 0){
			bids[i] = bids[i]/2;
		}
		while(bids[i]%3 == 0){
			bids[i] = bids[i]/3;
		}
	}
	sort(bids, bids+n);
	if(bids[0] == bids[n-1]){
		printf("Yes");
	}
	else{
		printf("No");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	for (int i = 2; i < n; i++)
	{
		ans = gcd(ans,a[i]);
		//mul /= ans;
		//mul *= a[i];
	}
	while (ans%2 == 0)
		ans = ans/2;
	while (ans%3 == 0)
		ans = ans/3;
//	mul *= 2;
	//mul = mul/ans;
	//cout << mul << endl;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		int ctr2 = 1;
		int ctr3 = 1;
		while (a[i]%2 == 0)
		{
			a[i] = a[i]/2;
			ctr2 = ctr2*2;
		}
		while (a[i]%3 == 0)
		{
			a[i] = a[i]/3;
			ctr3 = ctr3*3;
		}
		if (ans*ctr2*ctr3 != a[i])
		{
			cout << "No";
			return 0;
		}
	}
	cout << "Yes";
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	long long int mul = 1;
	mul = a[0]*a[1];
	mul = mul/ans;
	for (int i = 2; i < n; i++)
	{
		ans = gcd(mul,a[i]);
		mul *= a[i];
		mul /= ans;
	}
	//mul = mul/ans;
	//cout << mul << endl;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		long long int k = mul/a[i];
		while (k%2 == 0)
		{
			k = k/2;
		}
		while (k%3 == 0)
		{
			k = k/3;
		}
		if (k == 1)
			continue;
		else
		{
			f = 1;
			break;
		}
	}
	if (f)
		cout << "No\n";
	else
		cout << "Yes\n";
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	long long int mul = 1;
	mul = a[0]*a[1];
	mul = mul/ans;
	for (int i = 2; i < n; i++)
	{
		ans = gcd(ans,a[i]);
		mul *= a[i];
		mul /= ans;
	}
	//mul = mul/ans;
	//cout << mul << endl;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		long long int k = mul/a[i];
		while (k%2 == 0)
		{
			k = k/2;
		}
		while (k%3 == 0)
		{
			k = k/3;
		}
		if (k == 1)
			continue;
		else
		{
			f = 1;
			break;
		}
	}
	if (f)
		cout << "No\n";
	else
		cout << "Yes\n";
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	for (int i = 2; i < n; i++)
	{
		ans = gcd(ans,a[i]);
		//mul /= ans;
		//mul *= a[i];
	}
	while (ans%2 == 0)
		ans = ans/2;
	while (ans%3 == 0)
		ans = ans/3;
//	mul *= 2;
	//mul = mul/ans;
	//cout << mul << endl;
//	cout << ans;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		//int ctr2 = 1;
		//int ctr3 = 1;
	//	int temp = a[i];
		while (a[i]%2 == 0)
		{
			a[i] = a[i]/2;
			//ctr2 = ctr2*2;
		}
		while (a[i]%3 == 0)
		{
			a[i] = a[i]/3;
			//ctr3 = ctr3*3;
		}
		if (a[i] != ans)
		{
			cout << "No";
			return 0;
		}
	}
	cout << "Yes";
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	long long int mul;
	mul = a[0];
	mul = mul/ans;
	mul *= a[1];
	for (int i = 2; i < n; i++)
	{
		ans = gcd(mul,a[i]);
		mul /= ans;
		mul *= a[i];
	}
//	mul *= 2;
	//mul = mul/ans;
	//cout << mul << endl;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		long long int k = mul/a[i];
		while (k%2 == 0)
		{
			k = k/2;
		}
		while (k%3 == 0)
		{
			k = k/3;
		}
		if (k == 1)
			continue;
		else
		{
			if (n == 1234)
			cout << mul << " " << a[i] << endl;
			f = 1;
			break;
		}
	}
	if (f)
		cout << "No\n";
	else
		cout << "Yes\n";
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	long long int mul;
	mul = a[0]*a[1];
	mul = mul/ans;
	for (int i = 2; i < n; i++)
	{
		ans = gcd(mul,a[i]);
		mul *= a[i];
		mul /= ans;
	}
//	mul *= 2;
	//mul = mul/ans;
	//cout << mul << endl;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		long long int k = mul/a[i];
		while (k%2 == 0)
		{
			k = k/2;
		}
		while (k%3 == 0)
		{
			k = k/3;
		}
		if (k == 1)
			continue;
		else
		{
			cout << mul << " " << a[i] << endl;
			f = 1;
			break;
		}
	}
	if (f)
		cout << "No\n";
	else
		cout << "Yes\n";
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	long long int mul;
	mul = a[0]*a[1];
	mul = mul/ans;
	for (int i = 2; i < n; i++)
	{
		ans = gcd(mul,a[i]);
		mul *= a[i];
		mul /= ans;
	}
//	mul *= 2;
	//mul = mul/ans;
	//cout << mul << endl;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		long long int k = mul/a[i];
		while (k%2 == 0)
		{
			k = k/2;
		}
		while (k%3 == 0)
		{
			k = k/3;
		}
		if (k == 1)
			continue;
		else
		{
			if (n == 1234)
			cout << mul << " " << a[i] << endl;
			f = 1;
			break;
		}
	}
	if (f)
		cout << "No\n";
	else
		cout << "Yes\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long int gcd(long long int a,long long int b)
{
	if (b == 0)
		return a;
	while (a%b != 0)
	{
		int c = a%b;
		a = b;
		b = c;
	}
	return b;
}
int main()
{
	int n;
	cin >> n;
	long long int a[n];
	for (int i = 0; i < n; i++)
		scanf("%lld",&a[i]);
	long long int ans = gcd(a[0],a[1]);
	long long int mul = 1;
	mul = a[0]*a[1];
	mul = mul/ans;
	for (int i = 2; i < n; i++)
	{
		ans = gcd(mul,a[i]);
		mul *= a[i];
		mul /= ans;
	}
	mul *= 2;
	//mul = mul/ans;
	//cout << mul << endl;
	int f = 0;
	for (int i = 0; i < n; i++)
	{
		long long int k = mul/a[i];
		while (k%2 == 0)
		{
			k = k/2;
		}
		while (k%3 == 0)
		{
			k = k/3;
		}
		if (k == 1)
			continue;
		else
		{
			f = 1;
			break;
		}
	}
	if (f)
		cout << "No\n";
	else
		cout << "Yes\n";
	return 0;

}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    int n,x[100002],gcd;
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    gcd = x[0];
    for(int i=1;i<n;i++) gcd = __gcd(gcd,x[i]);
    for(int i=0;i<n;i++){
        int tmp = x[i]/gcd;
        while(tmp%2==0) tmp/=2;
        while(tmp%3==0) tmp/=3;
        if(tmp!=1){
            cout<<"No"<<endl;
            return 0;
        }
    }
    cout<<"Yes"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> p;
map<int,bool> h;
map<int,map<int,int> > pfc;
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= n; i = i+2)
    {
        if(i<3) break;
        while (n%i == 0)
        {
            if(i!=3) pfc[m][i]++;
            n = n/i;
        }
    }
    if(m==n) p[m]=1;
    if (n > 2) pfc[m][n]++;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    int n,x[10002];
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i]==x[i-1]) continue;
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        if(p[x[i]] && x[i]!=2 && x[i]!=3){
            if(x[i]!= x[i-1]) {f = 0;break;}
            if(i+1<n) if(x[i]!=x[i+1]){f=0;break;}
        }
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> p;
map<int,bool> h;
map<int,map<int,int> > pfc;
int n,x[100002];
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= n; i = i+2)
    {
        if(i<3) break;
        while (n%i == 0)
        {
            if(i!=3) pfc[m][i]++;
            n = n/i;
        }
    }
    if(m==n) p[m]=1;
    if (n > 2) pfc[m][n]++;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i]==x[i-1]) continue;
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        if(p[x[i]] && x[i]!=2 && x[i]!=3){
            if(x[i]!= x[i-1]) {f = 0;break;}
            if(i+1<n) if(x[i]!=x[i+1]){f=0;break;}
        }
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> p;
map<int,bool> h;
map<int,map<int,int> > pfc;
int n,x[100002];
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= n; i = i+2)
    {
        if(i<3) break;
        int c = 0;
        while (n%i == 0)
        {
            if(i!=3) c++;
            n = n/i;
        }
        pfc[m][i] = c;
    }
    if(m==n) p[m]=1;
    if (n > 2) pfc[m][n]++;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i]==x[i-1]) continue;
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        if(p[x[i]] && x[i]!=2 && x[i]!=3){
            if(x[i]!= x[i-1] && p[x[i-1]]) {f = 0;break;}
        }
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> h;
map<int,map<int,int> > pfc;
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= n; i = i+2)
    {
        if(i<3) break;
        while (n%i == 0)
        {
            if(i!=3) pfc[m][i]++;
            n = n/i;
        }
    }
    if (n > 2) pfc[m][n]++;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    int n,x[10002];
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i]==x[i-1]) continue;
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> p;
map<int,map<int,int> > pfc;
bool h[1000000004]={0};
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= n; i = i+2)
    {
        while (n%i == 0)
        {
            if(i!=3) pfc[m][i]++;
            n = n/i;
        }
    }
    if (n > 2) p[m] = 1;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    int n,x[10002];
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        if(p[x[i]] && x[i]!=2 && x[i]!=3){
            if(x[i]!= x[i-1]) {f = 0;break;}
            if(i+1<n) if(x[i]!=x[i+1]){f=0;break;}
        }
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> p;
map<int,bool> h;
map<int,map<int,int> > pfc;
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= n; i = i+2)
    {
        while (n%i == 0)
        {
            if(i!=3) pfc[m][i]++;
            n = n/i;
        }
    }
    if (n > 2) p[m] = 1;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    int n,x[10002];
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        if(p[x[i]] && x[i]!=2 && x[i]!=3){
            if(x[i]!= x[i-1]) {f = 0;break;}
            if(i+1<n) if(x[i]!=x[i+1]){f=0;break;}
        }
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    int n,x[10002],gcd;
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    gcd = x[0];
    for(int i=1;i<n;i++) gcd = __gcd(gcd,x[i]);
    for(int i=0;i<n;i++){
        int tmp = x[i]/gcd;
        while(tmp%2==0) tmp/=2;
        while(tmp%3==0) tmp/=3;
        if(tmp!=1){
            cout<<"No"<<endl;
            return 0;
        }
    }
    cout<<"Yes"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> p;
map<int,bool> h;
map<int,map<int,int> > pfc;
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= n; i = i+2)
    {
        while (n%i == 0)
        {
            if(i!=3) pfc[m][i]++;
            n = n/i;
        }
    }
    if (n > 2) p[m] = 1;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    int n,x[10002];
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i]==x[i-1]) continue;
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        if(p[x[i]] && x[i]!=2 && x[i]!=3){
            if(x[i]!= x[i-1]) {f = 0;break;}
            if(i+1<n) if(x[i]!=x[i+1]){f=0;break;}
        }
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include<bits/stdc++.h>
#define MAX 10000
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define ellapse printf("Time : %0.3lf\n",clock()*1.0/CLOCKS_PER_SEC);
using namespace std;
typedef long long l64d;
typedef unsigned long int ud;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
map<int,bool> p;
map<int,bool> h;
map<int,map<int,int> > pfc;
void factor(int n)
{
    int m = n;
    h[n] = 1;
    while (n%2 == 0) n = n/2;
    for (int i = 3; i * i <= m; i = i+2)
    {
        if(i<3) break;
        while (n%i == 0)
        {
            if(i!=3) pfc[m][i]++;
            n = n/i;
        }
    }
    if (n > 2) p[m] = 1;
}
int main()
{
    #ifdef Xanxiver
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    //std::ios::sync_with_stdio(false);
    bool f=1;
    int n,x[10002];
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i];
    sort(x,x+n);
    factor(x[0]);
    for(int i=1;i<n;i++){
        if(x[i]==1){
            if(x[i]==x[i-1]) continue;
            if(x[i-1]%2!=0 && x[i-1]%3!=0) {f=0;break;}
            if(i+1<n) if(x[i+1]%2!=0 && x[i-1]%3!=0){f=0;break;}
        }
        if(!h[x[i]]) factor(x[i]);
        if(p[x[i]] && x[i]!=2 && x[i]!=3){
            if(x[i]!= x[i-1]) {f = 0;break;}
            if(i+1<n) if(x[i]!=x[i+1]){f=0;break;}
        }
        for(int j=5;j*j<=x[i];j+=2){
            //cout<<j<<" : "<<pfc[x[i]][j]<<" -> "<<pfc[x[i-1]][j]<<endl;
            if(pfc[x[i]][j]!=pfc[x[i-1]][j]){f=0;break;}
        }
    }
    if(f) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    #ifdef Xanxiver
    ellapse;
    #endif // Xanxiver
}


#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int lcm(int a,int b){return ((a*b)/gcd(a,b));}

long long a[100000];
int main()
{
    long long i,j,k,l,m,n;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
                if((lcm(a[i],a[j])/a[i]>2)||(lcm(a[i],a[j])/a[j])>3)
            {
                cout << "NO" << endl;
                return 0;
            }
            }
            else
            {
                    if((lcm(a[i],a[j])/a[i]>3)||(lcm(a[i],a[j])/a[j])>2)
            {
                cout << "NO" << endl;
                return 0;
            }
            }

        }
    }
    cout << "YES" << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

long long gcd(int a,int b){return b==0?a:gcd(b,a%b);}
long long lcm(int a,int b){return ((a*b)/gcd(a,b));}

long long a[100000];
int main()
{
    long long i,j,k,l,m,n,maxx=0,g=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(lcm(a[i],a[j])>maxx)
                maxx=lcm(a[i],a[j]);
        }
    }
   // cout << maxx << endl;
    for(i=0;i<n;i++)
    {
            if((((maxx/a[i])%2)!=0)&&(((maxx/a[i])%3)!=0)&&(a[i]!=maxx))
            {
                cout << "No";
                return 0;
            }

    }
    cout << "YES" << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

long long gcd(int a,int b){return b==0?a:gcd(b,a%b);}
long long lcm(int a,int b){return ((a*b)/gcd(a,b));}

long long a[100000];
int main()
{
    long long i,j,k,l,m,n,maxx=0,g=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
        if(i==1)
        {
            maxx=lcm(a[i],a[0]);
        }
        if(i>1)
        {
            maxx=lcm(maxx,a[i]);
        }

    }

    for(i=0;i<n;i++)
    {
        j=a[i];
        while(a[i]<maxx)
        {
            a[i]*=2;
            if(a[i]==maxx)
                g=1;
        }
        a[i]=j;
        if((g==1)||(a[i]==maxx))
           {
               g=0;
               continue;
           }
        else
        {
                while(a[i]<maxx)
                {
                    a[i]*=3;
                    if(a[i]==maxx)
                        g=1;
                }
            if(g==1)
            {
                g=0;
                continue;
            }
            else
            {
                cout << "NO" << endl;
                return 0;
            }

        }
    }
    cout << "YES" << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

long long gcd(int a,int b){return b==0?a:gcd(b,a%b);}
long long lcm(int a,int b){return ((a*b)/gcd(a,b));}

long long a[100000];
int main()
{
    long long i,j,k,l,m,n;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]<a[j])
            {
                if(((lcm(a[i],a[j])/a[i])%3!=0)&&((lcm(a[i],a[j])/a[i])%2!=0)&&(a[i]!=a[j]))
                {
                    cout << "NO" << endl;
                    return 0;
                }
            }
            else
            {
                if(((lcm(a[i],a[j])/a[j])%3!=0)&&((lcm(a[i],a[j])/a[j])%2!=0)&&(a[i]!=a[j]))
                {
                    cout << "NO" << endl;
                    return 0;
                }

            }
        }

    }
    cout << "YES" << endl;
    return 0;
}


#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int lcm(int a,int b){return ((a*b)/gcd(a,b));}

long long a[100000];
int main()
{
    long long i,j,k,l,m,n;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]<a[j])
            {
                if((gcd((lcm(a[i],a[j])/a[i]),2)==1)&&(gcd((lcm(a[i],a[j])/a[i]),3)==1)&&(a[i]!=a[j]))
                {
                    cout << "NO" << endl;
                    return 0;
                }
            }
            else
            {
                if((gcd((lcm(a[i],a[j])/a[j]),2)==1)&&(gcd((lcm(a[i],a[j])/a[j]),3)==1)&&(a[i]!=a[j]))
                {
                    cout << "NO" << endl;
                    return 0;
                }

            }
        }

    }
    cout << "YES" << endl;
    return 0;
}