Well ive tested it and should work but here it is :

write("Distance: ")

local d=tonumber(read())
print("Starting Tunnel")
while d > 0 do
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.up()
turtle.turnLeft()
turtle.dig()
turtle.down()
turtle.dig()
turtle.down()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.up()
turtle.dig()
turtle.up()
turtle.dig()
turtle.down()
turtle.down()
turtle.turnLeft()
d= d - 1
end
print("Finished")

Thats it i hope you enjoy