1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <cmath>
5 #include <deque>
6 #include <vector>
7 #include <queue>
8 #include <iostream>
9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 #define For(i, s, t) for(int i = (s); i <= (t); i++)
16 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
17 #define Rep(i, t) for(int i = (0); i < (t); i++)
18 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
19 #define MIT (2147483647)
20 #define INF (1000000001)
21 #define MLL (1000000000000000001LL)
22 #define sz(x) ((int) (x).size())
23 #define clr(x, y) memset(x, y, sizeof(x))
24 #define puf push_front
25 #define pub push_back
26 #define pof pop_front
27 #define pob pop_back
28 #define ft first
29 #define sd second
30 #define mk make_pair
31 inline void SetIO(string Name) {
32 string Input = Name+".in",
33 Output = Name+".out";
34 freopen(Input.c_str(), "r", stdin),
35 freopen(Output.c_str(), "w", stdout);
36 }
37
38 const int N = 1010, M[6] = {1, 5, 10, 20, 50, 100};
39 const int G[6] = {1, 5, 10, 10, 50, 100};
40 typedef pair<int, int> II;
41 int Sum, Debt[3], St[3], Ed[3], Cash[3][6];
42 int Dp[2][N][N], Visit[2][N][N];
43 queue<II> Q[2];
44 int Ans = INF;
45
46 inline void Input() {
47 Rep(i, 3) scanf("%d", &Debt[i]);
48 Rep(i, 3)
49 Rep(j, 6)
50 scanf("%d", &Cash[i][5-j]);
51 }
52
53 inline void Push(int w, int A, int B, int Step) {
54 if(w >= 0) {
55 int Last[3], C = Sum-A-B;
56 Rep(i, 3) Last[i] = Ed[i];
57 Last[0] -= A, Last[1] -= B, Last[2] -= C;
58 Rep(i, 3)
59 if(Last[i]%G[w]) return;
60 }
61
62 int p = (w+2)%2;
63 if(Visit[p][A][B] == w) Dp[p][A][B] = min(Dp[p][A][B], Step);
64 else Q[p].push(mk(A, B)), Dp[p][A][B] = Step, Visit[p][A][B] = w;
65 if(A == Ed[0] && B == Ed[1]) Ans = min(Ans, Step);
66 }
67
68 inline void Solve() {
69 Rep(i, 3) {
70 Rep(j, 6) St[i] += Cash[i][j]*M[j];
71 Sum += St[i], Ed[i] += St[i];
72 Ed[i] -= Debt[i], Ed[(i+1)%3] += Debt[i];
73 }
74 Rep(i, 2)
75 Rep(j, Sum)
76 Rep(k, Sum) Visit[i][j][k] = -2;
77
78 Push(-1, St[0], St[1], 0);
79 int Last = 0, S[3], _Step, To, F1, F2, Front, T1, T2;
80 Rep(w, 6) {
81 Last ^= 1;
82 while(sz(Q[Last])) {
83 II State = Q[Last].front();
84 Q[Last].pop();
85
86 // One Get Front Two
87 Rep(i, 3) {
88 To = i, F1 = (i+1)%3;
89 F2 = 0+1+2-F1-To;
90 Rep(C1, Cash[F1][w]+1)
91 Rep(C2, Cash[F2][w]+1) {
92 S[0] = State.ft, S[1] = State.sd;
93 S[2] = Sum-S[0]-S[1];
94
95 _Step = Dp[Last][S[0]][S[1]]+C1+C2;
96 S[To] += (C1+C2)*M[w],
97 S[F1] -= C1*M[w], S[F2] -= C2*M[w];
98 Push(w, S[0], S[1], _Step);
99 }
100 }
101
102 // One Give To Two
103 Rep(i, 3) {
104 Front = i, T1 = (i+1)%3;
105 T2 = 0+1+2-Front-T1;
106 Rep(C1, Cash[Front][w]+1)
107 Rep(C2, Cash[Front][w]-C1+1) {
108 S[0] = State.ft, S[1] = State.sd;
109 S[2] = Sum-S[0]-S[1];
110
111 _Step = Dp[Last][S[0]][S[1]]+C1+C2;
112 S[Front] -= (C1+C2)*M[w];
113 S[T1] += C1*M[w], S[T2] += C2*M[w];
114 Push(w, S[0], S[1], _Step);
115 }
116 }
117 }
118 }
119
120 if(Ans == INF) puts("impossible");
121 else printf("%d\n", Ans);
122 }
123
124 int main() {
125 SetIO("1021");
126 Input();
127 Solve();
128 return 0;
129 }