Problem: You are given a straight half-line divided into segments of unit length, which we will call positions. The positions are numbered by positive integers that start with 1 from the end of half-line, i. e. 1, 2, 3 and so on. The distance between the positions is the absolute difference between the respective numbers. 

Laharl, Etna and Flonne occupy some positions on the half-line and they want to get to the position with the largest possible number. They are originally placed in different positions. 

Each of the characters can perform each of the following actions no more than once: 

  * Move a certain distance. 
  * Grab another character and lift him above the head. 
  * Throw the lifted character a certain distance. 



Each character has a movement range parameter. They can only move to free positions, assuming that distance between those positions doesn't exceed the movement range. 

One character can lift another character if the distance between the two characters equals 1, and no one already holds that another character. We can assume that the lifted character moves to the same position as the person who has lifted him, and the position in which he stood before becomes free. A lifted character cannot perform any actions and the character that holds him cannot walk. 

Also, each character has a throwing range parameter. It is the distance at which this character can throw the one lifted above his head. He can only throw a character to a free position, and only when there is a lifted character. 

We accept the situation when one person grabs another one who in his turn has the third character in his hands. This forms a "column" of three characters. For example, Laharl can hold Etna while Etna holds Flonne. In this case, Etna and the Flonne cannot perform any actions, and Laharl can only throw Etna (together with Flonne) at some distance. 

Laharl, Etna and Flonne perform actions in any order. They perform actions in turns, that is no two of them can do actions at the same time.

Determine the maximum number of position at least one of the characters can reach. That is, such maximal number x so that one of the characters can reach position x.

Input

The first line contains three integers: Laharl's position, his movement range and throwing range. The second and the third lines describe Etna's and Flonne's parameters correspondingly in the similar form. It is guaranteed that the three characters occupy distinct positions. All numbers in the input are between 1 and 10, inclusive.

Output

Print a single number — the maximum ordinal number of position which either Laharl, Etna or Flonne can reach.

Examples

Input

9 3 3
4 3 1
2 3 3


Output

15

Note

Let us explain how to reach position 15 in the sample.

Initially Laharl occupies position 9, Etna — position 4 and Flonne — position 2.

First Laharl moves to position 6.

Then Flonne moves to position 5 and grabs Etna.

Laharl grabs Flonne and throws to position 9.

Flonne throws Etna to position 12.

Etna moves to position 15.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int jdz(int a) { return a > 0 ? a : -a; }
int plc[4], mve[4], thr[4];
bool dp[45][45][45][8][8][8];
int ans;
struct node {
  int i, j, k, si, sj, sk;
  node() {}
  node(int a, int b, int c, int d, int e, int f) {
    i = a;
    j = b;
    k = c;
    si = d;
    sj = e;
    sk = f;
  }
};
queue<node> q;
int main() {
  for (int i = 1; i <= 3; i++) scanf("%d%d%d", &plc[i], &mve[i], &thr[i]);
  node st;
  int &i = st.i, &j = st.j, &k = st.k;
  int &si = st.si, &sj = st.sj, &sk = st.sk;
  i = plc[1] - 1;
  j = plc[2] - 1;
  k = plc[3] - 1;
  si = sj = sk = 0;
  q.push(st);
  dp[plc[1] - 1][plc[2] - 1][plc[3] - 1][0][0][0] = 1;
  while (!q.empty()) {
    st = q.front();
    q.pop();
    if (ans < max(max(i + 1, j + 1), k + 1))
      ans = max(max(i + 1, j + 1), k + 1);
    bool *nxt;
    if ((si == 0 || si == 1) && (sj % 4) != 1 && (sk % 4) != 1)
      for (int p = max(0, i - mve[1]); p <= i + mve[1]; p++)
        if (j != p && k != p) {
          nxt = &dp[p][j][k][si + 4][sj][sk];
          if (!*nxt) *nxt = 1, q.push(node(p, j, k, si + 4, sj, sk));
        }
    if ((sj == 0 || sj == 2) && (si % 4) != 2 && (sk % 4) != 2)
      for (int p = max(0, j - mve[2]); p <= j + mve[2]; p++)
        if (i != p && k != p) {
          nxt = &dp[i][p][k][si][sj + 4][sk];
          if (!*nxt) *nxt = 1, q.push(node(i, p, k, si, sj + 4, sk));
        }
    if ((sk == 0 || sk == 3) && (si % 4) != 3 && (sj % 4) != 3)
      for (int p = max(0, k - mve[3]); p <= k + mve[3]; p++)
        if (i != p && j != p) {
          nxt = &dp[i][j][p][si][sj][sk + 4];
          if (!*nxt) *nxt = 1, q.push(node(i, j, p, si, sj, sk + 4));
        }
    if ((si % 4) == 2 && (sk % 4) != 1)
      for (int p = max(0, j - thr[1]); p <= j + thr[1]; p++)
        if (i != p && k != p) {
          nxt = &dp[i][p][(sj) % 4 == 3 ? p : k][si - 1][sj][sk];
          if (!*nxt)
            *nxt = 1, q.push(node(i, p, (sj) % 4 == 3 ? p : k, si - 1, sj, sk));
        }
    if ((si % 4) == 3 && (sj % 4) != 1)
      for (int p = max(0, k - thr[1]); p <= k + thr[1]; p++)
        if (i != p && j != p) {
          nxt = &dp[i][(sk) % 4 == 2 ? p : j][p][si - 2][sj][sk];
          if (!*nxt)
            *nxt = 1, q.push(node(i, (sk) % 4 == 2 ? p : j, p, si - 2, sj, sk));
        }
    if ((sj % 4) == 1 && (sk % 4) != 2)
      for (int p = max(0, i - thr[2]); p <= i + thr[2]; p++)
        if (j != p && k != p) {
          nxt = &dp[p][j][(si) % 4 == 3 ? p : k][si][sj + 1][sk];
          if (!*nxt)
            *nxt = 1, q.push(node(p, j, (si) % 4 == 3 ? p : k, si, sj + 1, sk));
        }
    if ((sj % 4) == 3 && (si % 4) != 2)
      for (int p = max(0, k - thr[2]); p <= k + thr[2]; p++)
        if (i != p && j != p) {
          nxt = &dp[(sk) % 4 == 1 ? p : i][j][p][si][sj - 1][sk];
          if (!*nxt)
            *nxt = 1, q.push(node((sk) % 4 == 1 ? p : i, j, p, si, sj - 1, sk));
        }
    if ((sk % 4) == 1 && (sj % 4) != 3)
      for (int p = max(0, i - thr[3]); p <= i + thr[3]; p++)
        if (j != p && k != p) {
          nxt = &dp[p][(si) % 4 == 2 ? p : j][k][si][sj][sk + 2];
          if (!*nxt)
            *nxt = 1, q.push(node(p, (si) % 4 == 2 ? p : j, k, si, sj, sk + 2));
        }
    if ((sk % 4) == 2 && (si % 4) != 3)
      for (int p = max(0, j - thr[3]); p <= j + thr[3]; p++)
        if (i != p && k != p) {
          nxt = &dp[(sj) % 4 == 1 ? p : i][p][k][si][sj][sk + 1];
          if (!*nxt)
            *nxt = 1, q.push(node((sj) % 4 == 1 ? p : i, p, k, si, sj, sk + 1));
        }
    if (jdz(i - j) == 1 && (si % 4) == 0 && ((sk % 4) == 3 || (sk % 4) == 0)) {
      nxt = &dp[i][i][(sj) % 4 == 3 ? i : k][si + 2][sj][sk];
      if (!*nxt)
        *nxt = 1, q.push(node(i, i, (sj) % 4 == 3 ? i : k, si + 2, sj, sk));
    }
    if (jdz(i - k) == 1 && (si % 4) == 0 && ((sj % 4) == 2 || (sj % 4) == 0)) {
      nxt = &dp[i][(sk) % 4 == 2 ? i : j][i][si + 3][sj][sk];
      if (!*nxt)
        *nxt = 1, q.push(node(i, (sk) % 4 == 2 ? i : j, i, si + 3, sj, sk));
    }
    if (jdz(j - i) == 1 && (sj % 4) == 0 && ((sk % 4) == 3 || (sk % 4) == 0)) {
      nxt = &dp[j][j][(si) % 4 == 3 ? j : k][si][sj + 1][sk];
      if (!*nxt)
        *nxt = 1, q.push(node(j, j, (si) % 4 == 3 ? j : k, si, sj + 1, sk));
    }
    if (jdz(j - k) == 1 && (sj % 4) == 0 && ((si % 4) == 1 || (si % 4) == 0)) {
      nxt = &dp[(sk) % 4 == 1 ? j : i][j][j][si][sj + 3][sk];
      if (!*nxt)
        *nxt = 1, q.push(node((sk) % 4 == 1 ? j : i, j, j, si, sj + 3, sk));
    }
    if (jdz(k - i) == 1 && (sk % 4) == 0 && ((sj % 4) == 2 || (sj % 4) == 0)) {
      nxt = &dp[k][(si) % 4 == 2 ? k : j][k][si][sj][sk + 1];
      if (!*nxt)
        *nxt = 1, q.push(node(k, (si) % 4 == 2 ? k : j, k, si, sj, sk + 1));
    }
    if (jdz(k - j) == 1 && (sk % 4) == 0 && ((si % 4) == 1 || (si % 4) == 0)) {
      nxt = &dp[(sj) % 4 == 1 ? k : i][k][k][si][sj][sk + 2];
      if (!*nxt)
        *nxt = 1, q.push(node((sj) % 4 == 1 ? k : i, k, k, si, sj, sk + 2));
    }
  }
  printf("%d\n", ans);
}