#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
const int DIGIT = 14 ;
int a[ DIGIT] [ 10 ] ;
unordered_map< int , long long > dp[ DIGIT] ;
long long pow10[ DIGIT] ;
long long dfs( int index, int z) {
if ( index == DIGIT) {
if ( z == 0 ) {
return 0 ;
}
return - 1 ;
}
// read Cache
if ( dp[ index] .count ( z) ) {
return dp[ index] [ z] ;
}
dp[ index] [ z] = - 1 ;
for ( int w = 1 ; w < 10 ; w++ ) {
// mui x 0
// add x z
int newx = z;
if ( newx < 0 || a[ index] [ 1 ] <= 0 ) {
continue ;
}
// mod x 26
newx % = a[ index] [ 1 ] ;
if ( a[ index] [ 2 ] == 0 ) {
continue ;
}
// div z 1
int newz = z / a[ index] [ 2 ] ;
// add x 12
newx + = a[ index] [ 3 ] ;
// eql x w
newx = newx == w ? 1 : 0 ;
// eql x 0
newx = newx == a[ index] [ 4 ] ? 1 : 0 ;
// mul y 0
// add y 25
int newy = a[ index] [ 6 ] ;
// mul y x
newy * = newx;
// add y 1
newy + = a[ index] [ 7 ] ;
// mul z y
newz * = newy;
// mul y 0
newy * = a[ index] [ 8 ] ;
// add y w
newy + = w;
// add y 4
newy + = a[ index] [ 9 ] ;
// mul y x
newy * = newx;
// add y z
newz + = newy;
long long tmp = dfs( index + 1 , newz) ;
if ( tmp == - 1 ) {
continue ;
}
dp[ index] [ z] = tmp + w * pow10[ DIGIT - 1 - index] ;
return dp[ index] [ z] ;
}
return dp[ index] [ z] ;
}
int main( ) {
pow10[ 0 ] = 1 ;
for ( int i = 1 ; i < DIGIT; i++ ) {
pow10[ i] = pow10[ i - 1 ] * 10 ;
}
string s;
for ( int i = 0 ; i < DIGIT; i++ ) {
// inp w
getline( cin , s) ;
// mul x 0
cin >> s >> s >> a[ i] [ 0 ] >> ws;
// add x z
getline( cin , s) ;
// mod x 26
cin >> s >> s >> a[ i] [ 1 ] >> ws;
// div z 1
cin >> s >> s >> a[ i] [ 2 ] >> ws;
// add x 12
cin >> s >> s >> a[ i] [ 3 ] >> ws;
// eql x w
getline( cin , s) ;
// eql x 0
cin >> s >> s >> a[ i] [ 4 ] >> ws;
// mul y 0
cin >> s >> s >> a[ i] [ 5 ] >> ws;
// add y 25
cin >> s >> s >> a[ i] [ 6 ] >> ws;
// mul y x
getline( cin , s) ;
// add y 1
cin >> s >> s >> a[ i] [ 7 ] >> ws;
// mul z y
getline( cin , s) ;
// mul y 0
cin >> s >> s >> a[ i] [ 8 ] >> ws;
// add y w
getline( cin , s) ;
// add y 4
cin >> s >> s >> a[ i] [ 9 ] >> ws;
// mul y x
getline( cin , s) ;
// add z y
getline( cin , s) ;
}
cout << dfs( 0 , 0 ) << endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dW5vcmRlcmVkX21hcD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBESUdJVCA9IDE0OwppbnQgYVtESUdJVF1bMTBdOwoKdW5vcmRlcmVkX21hcDxpbnQsIGxvbmcgbG9uZz4gZHBbRElHSVRdOwoKbG9uZyBsb25nIHBvdzEwW0RJR0lUXTsKCmxvbmcgbG9uZyBkZnMoaW50IGluZGV4LCBpbnQgeikgewoJaWYgKGluZGV4ID09IERJR0lUKSB7CgkJaWYgKHogPT0gMCkgewoJCQlyZXR1cm4gMDsKCQl9CgkJcmV0dXJuIC0xOwoJfQoJLy8gcmVhZCBDYWNoZQoJaWYgKGRwW2luZGV4XS5jb3VudCh6KSkgewoJCXJldHVybiBkcFtpbmRleF1bel07Cgl9CglkcFtpbmRleF1bel0gPSAtMTsKCWZvciAoaW50IHcgPSAxOyB3IDwgMTA7IHcrKykgewoJCS8vIG11aSB4IDAKCQkvLyBhZGQgeCB6CgkJaW50IG5ld3ggPSB6OwoJCWlmIChuZXd4IDwgMCB8fCBhW2luZGV4XVsxXSA8PSAwKSB7CgkJCWNvbnRpbnVlOwoJCX0KCQkvLyBtb2QgeCAyNgoJCW5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ogeQppbnAgdwptdWwgeCAwCmFkZCB4IHoKbW9kIHggMjYKZGl2IHogMjYKYWRkIHggLTUKZXFsIHggdwplcWwgeCAwCm11bCB5IDAKYWRkIHkgMjUKbXVsIHkgeAphZGQgeSAxCm11bCB6IHkKbXVsIHkgMAphZGQgeSB3CmFkZCB5IDMKbXVsIHkgeAphZGQgeiB5CmlucCB3Cm11bCB4IDAKYWRkIHggegptb2QgeCAyNgpkaXYgeiAyNgphZGQgeCAtMgplcWwgeCB3CmVxbCB4IDAKbXVsIHkgMAphZGQgeSAyNQptdWwgeSB4CmFkZCB5IDEKbXVsIHogeQptdWwgeSAwCmFkZCB5IHcKYWRkIHkgMQptdWwgeSB4CmFkZCB6IHkKaW5wIHcKbXVsIHggMAphZGQgeCB6Cm1vZCB4IDI2CmRpdiB6IDI2CmFkZCB4IC03CmVxbCB4IHcKZXFsIHggMAptdWwgeSAwCmFkZCB5IDI1Cm11bCB5IHgKYWRkIHkgMQptdWwgeiB5Cm11bCB5IDAKYWRkIHkgdwphZGQgeSA4Cm11bCB5IHgKYWRkIHogeQ==
inp w
mul x 0
add x z
mod x 26
div z 1
add x 12
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 4
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 11
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 10
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 12
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -6
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 14
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 15
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 6
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 12
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 16
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -9
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 1
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 7
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 8
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -5
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -9
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 8
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -5
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 3
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -2
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 1
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -7
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 8
mul y x
add z y