```
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
int i = 0;
int tank = 0;
int t = 0;
for (i = 0; i < n + t; i++) {
tank += gas[i % n];
if (tank >= cost[i % n]) {
tank -= cost[i % n];
if (i == n + t - 1) {
return t;
}
} else {
tank = 0;
if (i >= n-1) {
break;
}
t = (i + 1) % n;
continue;
}
}
return -1;
}
```