#include <cstdio>

int T[7][10] =
{
    {0,1,2,3,4,5,6,0,1,2}, //0
    {3,4,5,6,0,1,2,3,4,5},
    {6,0,1,2,3,4,5,6,0,1},
    {2,3,4,5,6,0,1,2,3,4}, //3
    {5,6,0,1,2,3,4,5,6,0},
    {1,2,3,4,5,6,0,1,2,3},
    {4,5,6,0,1,2,3,4,5,6}
}, c = 0, D[10], D2[10], cnt, p;
char a[1000001], A[1000001];
int main()
{
    FILE *inn = fopen("./bbs/prob/893/input.txt","r");
    FILE *in  = fopen("./bbs/prob/893/output.txt","r");
    fscanf(inn,"%s", a);
    fscanf(in, "%s", A);
    for(int i = 0 ; a[i] ; i++ ) D[a[i]-'0']++;
    for(int i = 0 ; A[i] ; i++ ) D2[A[i]-'0']++;
    for(int i = 0 ; i < 10 ; i++ )
        if( D[i] != D2[i] ){
            puts("0");
            return 0;
        }
    if(A[0] == '0'){ puts("0"); return 0; }
    for(int i = 0 ; A[i] ; i++ ){
        c = T[c][A[i]-'0'];
    }
    printf("%d", c?0:1);
}
