// Author: Ivan Kazmenko (gassa@mail.ru)
module solution;
// version = IO_FILES;
import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
immutable string PROBLEM_NAME = "lights";
alias Lamp = Tuple !(int, q{x}, int, q{r});
bool check (Lamp [] lamps)
{
int limit = lamps.front.x;
foreach (ref lamp; lamps)
{
if (lamp.x > limit)
{
return false;
}
limit = max (limit, lamp.x + lamp.r);
}
return true;
}
void main ()
{
version (IO_FILES)
{
stdin = File (PROBLEM_NAME ~ ".in", "rt");
stdout = File (PROBLEM_NAME ~ ".out", "wt");
}
int n;
while (readf (" %s", &n) > 0)
{
auto lamps = new Lamp [n];
foreach (ref lamp; lamps)
{
readf (" %s %s", &lamp.x, &lamp.r);
}
auto lampsRev = lamps.retro
.map !(lamp => Lamp (-lamp.x, lamp.r)).array;
writeln (check (lamps) && check (lampsRev) ?
(lamps.back.x - lamps.front.x) * 3L : -1);
}
}
Ly8gQXV0aG9yOiBJdmFuIEthem1lbmtvIChnYXNzYUBtYWlsLnJ1KQptb2R1bGUgc29sdXRpb247Ci8vIHZlcnNpb24gPSBJT19GSUxFUzsKaW1wb3J0IHN0ZC5hbGdvcml0aG07CmltcG9ydCBzdGQucmFuZ2U7CmltcG9ydCBzdGQuc3RkaW87CmltcG9ydCBzdGQudHlwZWNvbnM7CgppbW11dGFibGUgc3RyaW5nIFBST0JMRU1fTkFNRSA9ICJsaWdodHMiOwoKYWxpYXMgTGFtcCA9IFR1cGxlICEoaW50LCBxe3h9LCBpbnQsIHF7cn0pOwoKYm9vbCBjaGVjayAoTGFtcCBbXSBsYW1wcykKewoJaW50IGxpbWl0ID0gbGFtcHMuZnJvbnQueDsKCWZvcmVhY2ggKHJlZiBsYW1wOyBsYW1wcykKCXsKCQlpZiAobGFtcC54ID4gbGltaXQpCgkJewoJCQlyZXR1cm4gZmFsc2U7CgkJfQoJCWxpbWl0ID0gbWF4IChsaW1pdCwgbGFtcC54ICsgbGFtcC5yKTsKCX0KCXJldHVybiB0cnVlOw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